简体   繁体   English

JPA / Hibernate OneToOne与相同PK的双向关系错误的插入顺序

[英]Wrong insert order at JPA/Hibernate OneToOne bidirectional relationship with the same PK

I'm trying to set up a OneToOne relationship between two entities that share the same PK: 我正在尝试在共享相同PK的两个实体之间建立OneToOne关系:

+----------------------------------+   +----------------+-----------------+        
|              Item                |   |              Stats               |
+----------------+-----------------+   +----------------+-----------------+
| reference (PK) |  other data...  |   | reference (PK) |  other data...  |
+----------------+-----------------+   +----------------+-----------------+
|          ABCD  |      ...        |   |          ABCD  |      ...        |
|           XYZ  |      ...        |   |           XYZ  |      ...        |
+----------------+-----------------+   +----------------+-----------------+

where Stats.reference is a FK to Item.reference : 其中Stats.reference是一个FK到Item.reference

alter table Stats 
    add constraint FK_8du3dv2q88ptdvthk8gmsipsk 
    foreign key (reference) 
    references Item;

This structure is generated from the following annotaded classes: 此结构由以下注释类生成:

@Entity
public class Item {
   @Id
   private String reference;

   @OneToOne(mappedBy = "item", optional = false)
   @Cascade({CascadeType.SAVE_UPDATE})
   @Fetch(FetchMode.SELECT)
   private Stats stats;

   // ...
}

@Entity
public class Stats {

   @Id
   @OneToOne
   @JoinColumn(name = "reference")
   @Fetch(FetchMode.SELECT)   
   private Item item;  

   // ...
}

A new Item is creted as follows: 一个新的Item如下:

Item item = new Item();
item.setReference("ABC");
Stats stats = new Stats();
stats.setItem(item);
item.setStats(stats);
session.save(item);

My problem is when I do session.save(item) the INSERT statements order are wrong. 我的问题是当我执行session.save(item) INSERT语句顺序错误时。 Hibernate first tries to insert into Stats table instead of Item , resulting in a FK constraint error. Hibernate首先尝试插入Stats表而不是Item ,导致FK约束错误。

How can I solve this issue? 我该如何解决这个问题? Thanks in advance. 提前致谢。

Since your class Stats owns the association, try to do : 由于您的类Stats拥有该关联,请尝试执行以下操作:

stats.setItem(item);
session.save(stats)

since you have one to one relationship between item and state and item has stats key as FK, and here you entity object should look like this 因为你在item和state之间有一对一的关系,而item的stats键为FK,这里你的实体对象应该是这样的

Item{ 项目{

@OneToOne
@JoinColumn(name = "stat_id", nullable=false) //incase if you allow null
private stats stats

} }

in stats entity no need to add relation annotation, since we defined in Item 在stats实体中不需要添加关系注释,因为我们在Item中定义了

while saving , you just save item entity, stats also get inserted 保存时,您只需保存项目实体,也可以插入统计数据

item.setStats(stats);
session.save(item);

in case if you want to save stats separately and items separately, 如果你想单独保存统计数据和单独保存项目,

then you have define cascade type as DETACH for stats 那么你已经将级联类型定义为stats的DETACH

 Class Item{

    @OneToOne(cascade=CascadeType.DETACH)
    @JoinColumn(name = "stat_id", nullable=false) //incase if you allow null
    private stats stats
}

The @JoinColumn annotation in Stats entity isn't complete, the attribute referencedColumnName must be specified. Stats实体中的@JoinColumn注释不完整,必须指定属性referencedColumnName

public abstract java.lang.String referencedColumnName public abstract java.lang.String referencedColumnName

(Optional) The name of the column referenced by this foreign key column. (可选)此外键列引用的列的名称。

@Entity
public class Stats {

   @Id
   @OneToOne
   @JoinColumn(name = "reference", referencedColumnName = "reference")
   @Fetch(FetchMode.SELECT)   
   private Item item;  

   // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM