简体   繁体   English

如何确保 hibernate 5 在与共享主键的一对一关系中以正确的顺序持续存在

[英]How to make sure hibernate 5 persists in the correct order in a one-to-one relationship with shared primary key

TLDR: Switched Hibernate Versions from 3. to 5. . TLDR:将 Hibernate 版本从 3. 切换到 5 .. Now, OneToOne Relationship with shared primary key that is only mapped in one class persists the two entities in the wrong order, violating a foreign key constraint.现在,具有仅映射在一个类中的共享主键的 OneToOne 关系以错误的顺序保留两个实体,违反了外键约束。 How to change the order?如何更改顺序?

In the process of migrating from a jboss 7 instance to a current wildfly, my team also had to update our hibernate version from 3 to 5.3.10.在从 jboss 7 实例迁移到当前的 wildfly 的过程中,我的团队还不得不将我们的 hibernate 版本从 3 更新到 5.3.10。

While doing that, I encountered a problem with one of our entity constructs in which I have hit a brick wall.在这样做时,我遇到了我们的一个实体构造的问题,其中我撞到了砖墙。

The general concept is the following:一般概念如下:

I have a main entity class that has multiple "modules" as attritubes which share the primary key of the main entity class.我有一个主实体类,它有多个“模块”作为属性,它们共享主实体类的主键。 These modules are entity classes themselves and hold various logic and further relationships.这些模块本身就是实体类,并持有各种逻辑和进一步的关系。 However, the module entities don't have any other persistent attributes.但是,模块实体没有任何其他持久属性。 They hold no reference to the main entity class either.它们也没有对主实体类的引用。

In that regard, it is (in the object oriented world of java) a unidirectional relationship with a shared primary key (I understand the shared primary key makes it bidirectional for the database).在这方面,它是(在 Java 的面向对象世界中)与共享主键的单向关系(我理解共享主键使其成为数据库的双向关系)。

The Java Code would be something like this: Java 代码将是这样的:

@Entity(name = "mainEntity")
@Table(name = "main_entity")
public class MainEntity {
// Business Logic ...


@Id
  private Long id;

  @OneToOne( targetEntity = ModuleA.class, cascade = CascadeType.ALL, optional = false,
      orphanRemoval = true )
  @PrimaryKeyJoinColumn( name = "id", referencedColumnName = "id" )
private ModuleA moduleA;
// More Modules...

}

@Entity(name=moduleA)
@Table(name=module_a)
public class ModuleA {
  @Id
  private Long id;
  // other relationships to entirely different entities,
  // but no reference to mainEntity or other "modules"
}

In our previous system, this worked just fine.在我们以前的系统中,这工作得很好。 When using something like当使用类似

entityManager.persist(mainEntity);

The modules would get persisted first, and afterwards the mainEntity.模块将首先被持久化,然后是 mainEntity。

However, with our updated hibernate version, this is not what happens.但是,在我们更新的休眠版本中,情况并非如此。 Using this mapping, the insertion order is reversed and the entityManager will try to persist the mainEntity first.使用此映射,插入顺序颠倒,entityManager 将首先尝试持久化 mainEntity。 This results in a violation of a foreign key constraint that should prevent inconsistencies between the tables.这会导致违反应防止表之间不一致的外键约束。

On a side note: I tried changing附注:我尝试改变

@PrimaryKeyJoinColumn(...)

into进入

@MapsId(value="id")

This actually changed the persitence order around correctly.这实际上正确地改变了持久性顺序。 However, doing this, caused hibernate to no longer properly understand the shared primary key -> While persisting worked for some reason,但是,这样做会导致 hibernate 不再正确理解共享主键 -> 虽然由于某种原因坚持工作,

entityManager.find(ModuleA.class,primaryKey);

did not work, yielding errors like "column mainEntity.moduleA_id does not exist."不起作用,产生诸如“列 mainEntity.moduleA_id 不存在”之类的错误。

Is there a way you can think of to express the need to persist the modules first, as Hibernate 3 did for us?有没有一种你能想到的方法来表达需要首先持久化模块,就像 Hibernate 3 为我们所做的那样?

I'll greatly appreciate any help.我将不胜感激任何帮助。

PS: This is my first question, if there's more information required, or if there's a problem with the formulation, do tell, please. PS:这是我的第一个问题,如果需要更多信息,或者配方有问题,请告诉。 :) :)

Alright, after some Analysis I wanted to share what we have done about this.好的,经过一些分析后,我想分享我们对此所做的工作。

Firstly, the mapping provided in the question works for Hibernate 3.6.10.Final up until Hibernate 5.2.13.Final where it starts to fail.首先,问题中提供的映射适用于 Hibernate 3.6.10.Final,直到 Hibernate 5.2.13.Final 开始失败。 Oddly, starting with 5.4.0.CR1 it starts working again.奇怪的是,从 5.4.0.CR1 开始它又开始工作了。

We have not found a way to continue using this mapping with the constraint in place.我们还没有找到在约束就位的情况下继续使用此映射的方法。 In our case, we have decided the constraint was just not worth the hassle, but to map this kind of thing more reliably, we have found that working with @MapsId was more successful.在我们的例子中,我们认为约束不值得麻烦,但为了更可靠地映射这种事情,我们发现使用 @MapsId 更成功。 There, the modules would need a reference to the MainEntity and would use @MapsId on that to derive its id.在那里,模块需要对 MainEntity 的引用,并在其上使用 @MapsId 来派生其 id。

暂无
暂无

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

相关问题 JPA / Hibernate使用共享主键进行单向一对一映射 - JPA / Hibernate unidirectional one-to-one mapping with shared primary key 在JPA中共享主键为一对一关系的情况下如何访问对象? - How to access object in case of shared primary key as one-to-one relationship in JPA? 如何在非主键上使用休眠注释设置单向一对一关系? - how to set unidirectional one-to-one relationship using hibernate annotation on a non-primary key? 如何正确级联在Hibernate 3.6中保存主键上的一对一双向关系 - How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6 休眠一对一映射,如何使两个实体具有相同的主键 - hibernate one-to-one mapping, how to make two entities have same primary key 具有共享主键的双向一对一关系的外键约束违规 - Foreign Key constraint violation with bidirectional one-to-one relationship with shared primary key Spring 引导数据 JPA:与共享主键的一对一关系 - Spring Boot Data JPA: One-to-One Relationship with Shared Primary Key 如何在不通过休眠中的共享主键以一对一关系插入子对象的情况下保存父实体? - How to save parent entity without inserting child in one to one relationship by shared primary key in hibernate? Hibernate One-To-One关系 - Hibernate One-To-One relationship JPA - 无法获得以一对一关系生成的主键 - JPA - Could not get primary key generated in a one-to-one relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM