简体   繁体   English

org.hibernate.id.IdentifierGenerationException:试图从空一对一属性(hibernate+postgres)分配id

[英]org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property (hibernate+postgres)

I'm using hibernate 5.3.11 with SpringDataJpa and PostgreSQL and I get "org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [packagename.ClassB.classA]" when trying to update a complex entity ClassC.我正在使用带有 SpringDataJpa 和 PostgreSQL 的 hibernate 5.3.11,当我尝试更新一个复杂的时,我得到“org.hibernate.id.IdentifierGenerationException:试图从空一对一属性 [packagename.ClassB.classA] 分配id”实体类 C.

  • ClassC is @OneToMany with ClassA ClassC 是 @OneToMany 和 ClassA
  • ClassA is @OneToOne with ClassB. ClassA 是带有 ClassB 的 @OneToOne。
  • All entities have links to each other before calling update method.在调用更新方法之前,所有实体都有彼此的链接。
  • ClassC is a parent entity and cascades to ClassA, which cascades to ClassB. ClassC 是一个父实体,级联到 ClassA,级联到 ClassB。
  • The issue seems to be related only to A<->B relationship.这个问题似乎只与 A<->B 关系有关。
  • show-sql tells me that ClassA persists fine, but seems like for some reason it looses link to ClassB somewhere in hibernate. show-sql 告诉我 ClassA 保持良好,但似乎由于某种原因它在休眠状态的某个地方失去了与 ClassB 的链接。
  • Persisting entity for the first time works fine.第一次持久化实体工作正常。 The error only comes on update.错误仅在更新时出现。 And I don't even update existing entities of ClassB and ClassA, I replace them with new ones.而且我什至不更新 ClassB 和 ClassA 的现有实体,而是用新实体替换它们。

Relationship:关系:

关系

ClassB in SQL : SQL中的B类 SQL中的B类

ClassA in SQL : SQL中的A类 SQL中的A类

Java entities: Java实体:

@Data
@Entity
@EqualsAndHashCode(exclude = {"classA"})
public class ClassA {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne
    @JoinColumn(name = "classC_id")
    private ClassC classC;
    
    @OneToOne(mappedBy = "classA", cascade = CascadeType.ALL)
    private ClassB classB;
}
    
@Data
@Entity
public class ClassB {
    
    @Id
    private Long id;
    
    @OneToOne
    @JoinColumn(name = "id")
    @MapsId
    private ClassA classA;
}

Short Answer: I used SpringBoot ver.2.1.8.简短回答:我使用了 SpringBoot ver.2.1.8。 Updating to 2.2.1 helped.更新到 2.2.1 有所帮助。 Long answer here Post-upgrade, @MapsId is throwing an error when saving an existing entity, but otherwise works fine长答案在这里升级后,@MapsId 在保存现有实体时抛出错误,但其他方面工作正常

暂无
暂无

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

相关问题 Hibernate merge org.hibernate.id.IdentifierGenerationException:尝试从空的一对一属性分配ID - Hibernate merge org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property org.hibernate.id.IdentifierGenerationException:“试图从空一对一属性分配 id” - org.hibernate.id.IdentifierGenerationException: "attempted to assign id from null one-to-one property" org.hibernate.id.IdentifierGenerationException:尝试从空的一对一属性(使用session.merge)分配ID - org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property (with session.merge) Hibernate 尝试从 null 一对一属性分配 id - Hibernate attempted to assign id from null one-to-one property 休眠一对零或一个映射。 例外:尝试从空的一对一属性分配ID? - Hibernate one to zero or one mapping. Exception: attempted to assign id from null one-to-one property? Hibernate 错误:试图从 null 一对一属性分配 id。 为什么? - Hibernate Error : attempted to assign id from null one-to-one property. Why? Hibernate OneToOne 尝试从 null 一对一属性分配 id - Hibernate OneToOne attempted to assign id from null one-to-one property org.hibernate.id.IdentifierGenerationException:同时将数据保存在一对一映射中 - org.hibernate.id.IdentifierGenerationException: while saving data in one to one mapping @EmbeddId 正在抛出 org.hibernate.id.IdentifierGenerationException: null id 在 ZCB1F008EEBF5012C4EF9A2C36E574D61 中使用.save() 函数时生成 - @EmbeddId is thowing org.hibernate.id.IdentifierGenerationException: null id generated while using .save() frunction in hibernate javax.persistence.PersistenceException:org.hibernate.id.IdentifierGenerationException: - javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM