Hibernate Many to one updating foreign key to null

rioubenson picture rioubenson · Mar 23, 2014 · Viewed 27.4k times · Source

I am trying to get my @OneToMany and @ManyToOne relationships correct.

Class 1:

@Entity
public class IdeaProfile {

@Id
@GeneratedValue
private int ideaProfileId;

private String name;

Date dateConcieved;

@OneToOne
@JoinColumn(name="statusCode")  
private Status status;


@OneToMany(fetch=FetchType.EAGER, targetEntity=Pitch.class, cascade=CascadeType.ALL)
@JoinColumn(name = "ideaProfileId") 
private List<Pitch> pitchs;

    ....getters and setters....

Class2:

@Entity
public class Pitch {

@Id
@GeneratedValue
private int id;

@ManyToOne
@JoinColumn(name = "ideaProfileId")
private IdeaProfile ideaProfile;

private Date date;

private String notes;

 ....getters and setters....

This relationship seems to work fine when I am loading or saving a new record:

Hibernate: insert into IdeaProfile (dateConcieved, genreCode, name, statusCode) values (?, ?, ?, ?)
Hibernate: insert into Pitch (date, ideaProfileId, notes) values (?, ?, ?)
Hibernate: update Pitch set ideaProfileId=? where id=?

However, when I try to update that record, it tries to set the IdeaProfileId to null:

Hibernate: update IdeaProfile set dateConcieved=?, genreCode=?, name=?, statusCode=?,  where ideaProfileId=?
Hibernate: update Pitch set date=?, ideaProfileId=?, notes=? where id=?
Hibernate: update Pitch set ideaProfileId=null where ideaProfileId=?

When I debug I can see that IdeaProfileId is indeed set on Pitch Object...

FYI, I am not directly updating the original objects that I loaded from the DB. These domains get mapped to a Model class which is what the UI updates. So on save/update I map the values back to new domain objects, including the IDs like the following:

IdeaProfile domain = new IdeaProfile();
domain.setId(model.getIdeaProfileId());
domain.setName(model.getName());
domain.setStatus(model.getStatus());
domain.setDateConcieved(Date.valueOf(model.getDateConvieved()));
for (PitchModel pitch : model.getPitches()) {
     Pitch pitchDomain = new Pitch();
     pitchDomain.setId(pitch.getId());
     pitchDomain.setDate(Date.valueOf(pitch.getDate()));
     pitchDomain.setNotes(pitch.getNotes());
     pitchDomain.setIdeaProfile(domain);
     if(domain.getPitchs() == null ) {
        domain.setPitchs(new ArrayList<Pitch>());
     }
     domain.getPitchs().add(pitchDomain);
 }

openSession();
session.beginTransaction();
session.saveOrUpdate(domain);
session.getTransaction().commit();
closeSession();

Does anyone know what I have done wrong so Hibernate causes the update to try to set IdeaProfileId to null?

Much appreciated.

Answer

JB Nizet picture JB Nizet · Mar 23, 2014

You don't have a bidirectional association here. You have two independent associations, each incorrectly mapped to the same column.

In a bidirectional association, you must always have an owner side, and an inverse side. The inverse side is marked using the mappedBy attribute. In a OneToMany association, the inverse side must be the one-side:

@OneToMany(mappedBy="ideaProfile", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<Pitch> pitchs;

...

@ManyToOne
@JoinColumn(name = "ideaProfileId")
private IdeaProfile ideaProfile;