Java Hibernate 多对一更新外键为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22591684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Many to one updating foreign key to null
提问by rioubenson
I am trying to get my @OneToMany and @ManyToOne relationships correct.
我试图让我的 @OneToMany 和 @ManyToOne 关系正确。
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:
类2:
@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:
但是,当我尝试更新该记录时,它会尝试将 IdeaProfileId 设置为 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...
当我调试时,我可以看到 IdeaProfileId 确实设置在 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:
仅供参考,我没有直接更新从数据库加载的原始对象。这些域被映射到模型类,这是 UI 更新的内容。因此,在保存/更新时,我将值映射回新的域对象,包括如下所示的 ID:
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?
有谁知道我做错了什么,所以 Hibernate 导致更新尝试将 IdeaProfileId 设置为 null?
Much appreciated.
非常感激。
采纳答案by JB Nizet
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:
在双向关联中,您必须始终具有所有者侧和反向侧。反面使用mappedBy 属性进行标记。在 OneToMany 关联中,反面必须是一侧:
@OneToMany(mappedBy="ideaProfile", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<Pitch> pitchs;
...
...
@ManyToOne
@JoinColumn(name = "ideaProfileId")
private IdeaProfile ideaProfile;