休眠多对一更新外键为空 [英] Hibernate Many to one updating foreign key to null
问题描述
我试图让我的@OneToMany和@ManyToOne关系正确。
1级:
@Entity
public class IdeaProfile {
@Id
@GeneratedValue
private int ideaProfileId;
私人字符串名称;
日期dateConcieved;
@OneToOne
@JoinColumn(name =statusCode)
私人状态状态;
$ b @OneToMany(fetch = FetchType.EAGER,targetEntity = Pitch.class,cascade = CascadeType.ALL)
@JoinColumn(name =ideaProfileId)
私人列表< Pitch> pitchs;
.... getters and setters ....
Class2:
@Entity
公共班级Pitch {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name =ideaProfileId)
私人IdeaProfile ideaProfile;
私人日期日期;
私人字符串笔记;
.... getters and setters ....
这种关系在加载或保存新记录时似乎工作正常:
$ b $ pre $ Hibernate:插入到IdeaProfile(dateConcieved,genreCode,name, Hibernate:插入Pitch(date,ideaProfileId,notes)值(?,?,?)
Hibernate:更新Pitch set ideaProfileId =?其中id =?
但是,当我尝试更新该记录时,它会尝试将IdeaProfileId设置为null:
Hibernate:更新IdeaProfile set dateConcieved = ?, genreCode = ?, name =?,statusCode = ?,其中ideaProfileId =?
Hibernate:更新音高设置日期=?,ideaProfileId =?,音符=?其中id =?
Hibernate:更新Pitch set ideaProfileId = null其中ideaProfileId =?
当我调试时,我可以看到IdeaProfileId确实设置在Pitch Object上...
仅供参考,我不直接更新从数据库加载的原始对象。这些域被映射到UI更新的Model类。所以在保存/更新时,我会将这些值映射回新的域对象,包括如下的ID:
IdeaProfile domain = new IdeaProfile();
domain.setId(model.getIdeaProfileId());
domain.setName(model.getName());
domain.setStatus(model.getStatus());
domain.setDateConcieved(Date.valueOf(model.getDateConvieved())); (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();
有谁知道我做错了什么,所以Hibernate导致更新尝试将IdeaProfileId设置为null?
非常感谢。
您没有双向协会在这里。您有两个独立的关联,每个关联都错误地映射到同一列。
在双向关联中,您必须始终拥有所有者方和反方。反面是使用mappedBy属性标记的。在OneToMany关联中,反面必须是单面:
@OneToMany(mappedBy =ideaProfile,fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private List< Pitch> pitchs;
...
@ManyToOne
@JoinColumn(name =ideaProfileId)
私人IdeaProfile ideaProfile;
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.
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;
这篇关于休眠多对一更新外键为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!