Hibernate Many to one updating foreign key to null
Asked Answered
T

1

4

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.

Trim answered 23/3, 2014 at 13:44 Comment(0)
O
18

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;
Ostracism answered 23/3, 2014 at 14:55 Comment(1)
The problem here is the performance, if you use EAGER you should study the table Pitch if there are a lot of rows, but is not a bad practise if is only a dict of values.Durkee

© 2022 - 2024 — McMap. All rights reserved.