【问题标题】:Hibernate doesn't create foreign keys in database for unidirectional ManyToOneHibernate 不会在数据库中为单向多对一创建外键
【发布时间】:2019-01-02 15:50:00
【问题描述】:

我有两个实体

@Entity
public class Address {

    @ManyToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name = "city_id")
    private City city;
}

@Entity
public class City {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "city_id")
    private int id;
}

在控制台中休眠打印创建了 City 表的外键

Hibernate: alter table Address add constraint FKpwa35mv5w9mb3syngd4m8fprw foreign key (city_id) references City (city_id)

,但我在数据库中看不到

谢谢。

【问题讨论】:

  • 你用的是什么数据库?
  • @Simon Martinelli MySQL
  • 您已经刷新了 UI SQL 编辑器中的表格。
  • 当然也可以运行显示外键的sql脚本
  • 你有 InnoDB 或 MyISAM 吗? MyISAM 不支持外键。

标签: hibernate jpa jpa-2.0


【解决方案1】:

您可能使用 MyISAM 作为存储。 MyISAM 不支持外键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    相关资源
    最近更新 更多