Nephisto
Nephisto

Reputation: 3

org.hibernate.PropertyValueException

i have these 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

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

but that is not what i want (participant should not be null!)...setting nullable=true create another exception

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 :-(

Upvotes: 0

Views: 5394

Answers (1)

Vineet Reynolds
Vineet Reynolds

Reputation: 76709

You have created a bi-directional relationship with ParticipantPojo acting as the defining side (with the mappedBy property in the @OneToMany annotation). The ParticipantIdentificationNumberPojo is the owning side of the relationship, and you must register the instance of the defining side on the owning side.

So, you will have to register the ParticipantPojo instance as follows:

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; once you think about this, you'll realize the reason for the second error message that you are getting - foreign keys cannot be null.

Upvotes: 1

Related Questions