[英]How to create table with unidirectional relation in Hibernate?
i have the empty database in mysql, and two java entites.我在 mysql 和两个 java 实体中有空数据库。 One of those have unidirectional relation.
其中之一具有单向关系。 When hibernate tryes to create tables, i got the error:
当 hibernate 尝试创建表时,出现错误:
Error executing DDL "alter table entry add constraint FK6ov2k83sx3crs9v3q8nvjuf1j foreign key (category_name) references category (name)" via JDBC Statement
There are my entites:有我的实体:
@Entity
public class Entry {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY)
private int id;
@Column
private String myfio;
private String descr;
@OneToOne(cascade = CascadeType.ALL)
private Category category;
}
And the second:第二个:
@Entity
@Table(name="category")
public class Category {
@Id
@Column
private String name;
}
How to create tables without errors?如何创建没有错误的表?
OneToOne relationship shares the same id. OneToOne 关系共享相同的 id。 So it should be the same type, but the first one is int (actually it should be Integer to allow null value for the transient (not stored) entities) and the second one is String.
所以它应该是相同的类型,但第一个是int (实际上应该是Integer以允许瞬态(未存储)实体的 null 值),第二个是 String。 It seems you simply missed a line.
看来您只是错过了一条线。 Also, it worths to mention Vlad Mihalchea's article https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/
此外,值得一提的是 Vlad Mihalchea 的文章https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.