[英]org.hibernate.PropertyValueException
i have these Pojos:我有这些 Pojos:
public class ParticipantPojo implements Serializable
.....
@OneToMany(mappedBy = "participant", cascade = CascadeType.ALL)
private Set<ParticipantIdentificationNumberPojo> identificationNumbers;
and和
public class ParticipantIdentificationNumberPojo implements Serializable
......
@ManyToOne
@JoinColumn(name = "PARTICIPANT", nullable = false)
private ParticipantPojo participant;
When i want to create a new participant with当我想创建一个新参与者时
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);
and save it并保存
session.save(pojo);
i get the following error:我收到以下错误:
org.hibernate.PropertyValueException: not-null property references a null or transient value: de.seeburger.marktpartnerdaten.database.pojo.ParticipantIdentificationNumberPojo.participant
In a blog i found the solution to set nullable=true at在博客中,我找到了设置 nullable=true 的解决方案
@JoinColumn(name = "PARTICIPANT", nullable = false)
but that is not what i want (participant should not be null.)...setting nullable=true create another exception但这不是我想要的(参与者不应该是 null。)...设置 nullable=true 创建另一个异常
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: de.seeburger.marktpartnerdaten.database.pojo.ParticipantPojo
I think the problem is, that Hibernate does not save the participant and take the ID to save participantIdentificationNumber...but i found no solution for my exception:-(我认为问题是,Hibernate 没有保存参与者并使用 ID 来保存参与者标识号......但我发现我的异常没有解决方案:-(
You have created a bi-directional relationship with ParticipantPojo
acting as the defining side (with the mappedBy
property in the @OneToMany
annotation).您已经创建了一个双向关系,
ParticipantPojo
作为定义方(使用@OneToMany
注释中的mappedBy
属性)。 The ParticipantIdentificationNumberPojo
is the owning side of the relationship, and you must register the instance of the defining side on the owning side. ParticipantIdentificationNumberPojo
是关系的拥有方,您必须在拥有方注册定义方的实例。
So, you will have to register the ParticipantPojo
instance as follows:因此,您必须按如下方式注册
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);
Ideally you must register the relationship in both directions even though you have declared a bi-directional relationship.理想情况下,即使您已声明双向关系,您也必须在两个方向上注册关系。 Hibernate will however make things easy if you set the value on the owning side, as that is the side that contains the foreign key;
但是,如果您在拥有方设置值,Hibernate 将使事情变得容易,因为那是包含外键的一方; once you think about this, you'll realize the reason for the second error message that you are getting - foreign keys cannot be null.
一旦您考虑到这一点,您就会意识到您收到第二条错误消息的原因 - 外键不能是 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.