gpt4 book ai didi

java - org.hibernate.PropertyValueException

转载 作者:行者123 更新时间:2023-12-01 15:48:14 25 4
gpt4 key购买 nike

我有这些 Pojo:

public class ParticipantPojo implements Serializable
.....
@OneToMany(mappedBy = "participant", cascade = CascadeType.ALL)
private Set<ParticipantIdentificationNumberPojo> identificationNumbers;

public class ParticipantIdentificationNumberPojo implements Serializable
......
@ManyToOne
@JoinColumn(name = "PARTICIPANT", nullable = false)
private ParticipantPojo participant;

当我想创建一个新参与者时

 ParticipantPojo pojo= new ParticipantPojo ();
...
Set<ParticipantIdentificationNumberPojo> identSet = new HashSet<ParticipantIdentificationNumberPojo>();
ParticipantIdentificationNumberPojo identInfo= new ParticipantIdentificationNumberPojo();
identInfo.setType("xyz");
identInfo.setNumber(12345);
identSet.add(identInfo);
pojo.setIdentificationNumbers(identSet);

并保存

 session.save(pojo);

我收到以下错误:

org.hibernate.PropertyValueException: not-null property references a null or transient value: de.seeburger.marktpartnerdaten.database.pojo.ParticipantIdentificationNumberPojo.participant

在博客中我找到了设置 nullable=true 的解决方案

@JoinColumn(name = "PARTICIPANT", nullable = false)

但这不是我想要的(参与者不应该为空!)...设置 nullable=true 创建另一个异常

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: de.seeburger.marktpartnerdaten.database.pojo.ParticipantPojo

我认为问题是,Hibernate 不会保存参与者并使用 ID 来保存参与者IdentificationNumber...但我没有找到针对我的异常的解决方案:-(

最佳答案

您已创建与作为定义方的 ParticipantPojo 的双向关系(使用 @OneToMany 注释中的 mappedBy 属性) 。 ParticipantIdentificationNumberPojo 是关系的拥有方,您必须在拥有方注册定义方的实例。

因此,您必须按如下方式注册 ParticipantPojo 实例:

ParticipantPojo pojo= new ParticipantPojo ();
...
Set<ParticipantIdentificationNumberPojo> identSet = new HashSet<ParticipantIdentificationNumberPojo>();
ParticipantIdentificationNumberPojo identInfo= new ParticipantIdentificationNumberPojo();
identInfo.setType("xyz");
identInfo.setNumber(12345);
identInfo.setParticipantPojo(pojo); // <-- Registering the instance on the owning side.
identSet.add(identInfo);
pojo.setIdentificationNumbers(identSet);

理想情况下,即使您已声明双向关系,您也必须在两个方向上注册该关系。然而,如果您在拥有方设置值,Hibernate 将使事情变得容易,因为那一方包含外键;一旦您考虑到这一点,您就会意识到收到第二条错误消息的原因 - 外键不能为空。

关于java - org.hibernate.PropertyValueException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com