Hibernate 不会在数据库中为单向 ManyToOne 创建外键

Hibernate doesn't create foreign keys in database for unidirectional ManyToOne

我有两个实体

@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;
}

Hibernate 在控制台中打印已创建城市外键 table

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

,但我在数据库中看不到

谢谢。

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