休眠多对一将外键更新为空

里奥本森

我正在尝试使我的@OneToMany和@ManyToOne关系正确。

第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....

第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....

当我加载或保存新记录时,这种关系似乎运行良好:

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

但是,当我尝试更新该记录时,它尝试将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=?

当我调试时,我可以看到IdeaProfileId确实在音高对象上设置了...

仅供参考,我不是直接更新从数据库加载的原始对象。这些域映射到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()));
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();

有谁知道我做错了什么,所以Hibernate导致更新尝试将IdeaProfileId设置为null?

非常感激。

尼伯(JB Nizet)

您在这里没有双向关联。您有两个独立的关联,每个关联都错误地映射到同一列。

在双向关联中,必须始终具有所有者侧和反向侧。反面是使用mapledBy属性标记的。在OneToMany关联中,反面必须是一侧:

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

...

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

休眠多对一将外键更新为空

来自分类Dev

将外键更新为空值

来自分类Dev

删除可选的一对多操作不会将外键设置为空

来自分类Dev

休眠一对多映射尝试将映射列更新为null

来自分类Dev

多对多休眠无法添加或更新子行:外键约束失败

来自分类Dev

无需外键但使用反向外键即可休眠多对一关系

来自分类Dev

无需外键但使用反向外键即可休眠多对一关系

来自分类Dev

使用CONCAT将空值从多列更新为一显示空值

来自分类Dev

将外键设置为可空之后,实体框架代码首先无法更新数据库

来自分类Dev

Hibernate一对多地将外键翻倍

来自分类Dev

休眠一对多关系无法为外键提供价值

来自分类Dev

外键更新异常:在Hibernate一对多关系中,外键设置为null

来自分类Dev

空外键(Springboot,休眠,邮递员)

来自分类Dev

休眠:映射指向唯一键的外键

来自分类Dev

如何为众多模板之一将Meteor助手设置为true?

来自分类Dev

使用图像列之一将Gridview数据导出为PDF

来自分类Dev

休眠外键错误

来自分类Dev

休眠外键在OrderItem(order_orderID)中为null

来自分类Dev

休眠外键在OrderItem(order_orderID)中为null

来自分类Dev

子表中的JPA外键为空

来自分类Dev

外键始终保持为空

来自分类Dev

扭矩4中的外键为空

来自分类Dev

mongodb includesTo外键为空

来自分类Dev

子表中的JPA外键为空

来自分类Dev

外键始终保持为空

来自分类Dev

休眠一对一映射外键null

来自分类Dev

休眠外键一对一单向

来自分类Dev

Hibernate 不会在数据库中为单向多对一创建外键

来自分类Dev

无法将具有独立关联的可选外键设置为空