我无法在“宠物”实体中生成外键。该模式已使用“ spring.jpa.hibernate.ddl-auto = create”成功生成,但是由于缺少外键,因此未链接我的表。
我在“ appliction.properties”中添加了“ hibernate.dialect.storage_engine = innodb”,这什么也没产生,我认为InnoDB现在仍然是默认引擎。
Pet.java
@Entity
@Table(name="pet")
public class Pet {
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private int id;
@Column(name="name")
private String name;
@Column(name="type_of_pet")
private String typeOfPet;
@ManyToOne(cascade={CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name="owner_id")
private Owner owner;
public Pet(String name, String typeOfPet) {
this.name = name;
this.typeOfPet = typeOfPet;
}
public Pet() {
}
Owner.java
@Entity
@Table(name="owner")
public class Owner {
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private int id;
@Column(name="name")
private String name;
@OneToMany(mappedBy="owner", cascade={CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private List<Pet> pets;
public Owner(String name) {
this.name = name;
}
public Owner() {
}
除了我在Pet表中期望的外键之外,所有内容都会生成。
答案 0 :(得分:0)
当我在“ application.properties”中添加“ spring.jpa.database-platform = org.hibernate.dialect.MySQL8Dialect”时,终于添加了外键。当我使用“ org.hibernate.dialect.MySQL5Dialect”(默认方言)时,这将无法工作。