简体   繁体   English

如何在 Hibernate 中创建具有单向关系的表?

[英]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.

相关问题 如何使用 Hibernate Search 以单向多对多和多对一关系连接数据 - How to join data in unidirectional ManyToMany and ManyToOne relation with Hibernate Search Hibernate单向多对多更新目标组成关系 - Hibernate Unidirectional ManyToMany Update Target Constituent Relation 外键关系:单向注释休眠 - Foreign key Relation : UniDirectional annotation hibernate Hibernate无法创建具有@ManyToOne关系的表 - Hibernate can't create a table with @ManyToOne relation 与JoinTable的单向OneToMany关系-未生成表 - Unidirectional OneToMany relation with JoinTable - table not generated 未映射表上的Hibernate查询内部联接(单向) - Hibernate query inner join on unmapped table (unidirectional) JPA / Hibernate:双向OneToMany / ManyToOne关系仅适用于单向 - JPA/Hibernate: bidirectional OneToMany/ManyToOne relation only works unidirectional 如何将@OneToOne 关系映射到 Hibernate 中的静态表 - How to map @OneToOne relation with a static table in Hibernate 如何在外键与联接表和单向休眠状态下进行一对一关联 - How to do One to One association in hibernate with foreign key and join table and unidirectional 如何防止仅在休眠中的子表中进行更新操作(一对多映射单向)? - How to prevent update operation only in child table in hibernate (one-to-many mapping unidirectional)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM