[英]Hibernate getting NULL in foreign key @oneToone mapping
我在两个类之间有这样的关系:
Smartlist -> smartlistId` 是 PK
AccountEmailing -> smartlistId FK,PK
AccountEmailing 表可能没有初始参考记录,但后来可能有记录
我只使用 smartlist 表存储库
我曾尝试过cascade.ALL
但在FK表ID中获得空值
我已经尝试了以下代码,它使用以下组合,
与初始数据
smartlist
和AccountEmailing
这是有效的,我在两个表中都有一条记录,但后来更新了一条记录,给我一个错误,因为 AccountEmailing 已经有一个条目( CascadeType.PERSIST
只允许插入而不是更新子项)
带有智能列表的初始数据,而 AccountEmailing 中没有数据
意味着它不会在 AccountEmailing 中创建一个条目,但稍后会添加一条记录,它会为 AccountEmailing 创建一个插入语句,并且从下次它总是更新
我正在寻找一种解决方案,如何更新我的课程,以便我可以在 AccountEmailing 上执行 CRUD:
public class Smartlist {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "smartlistId")
private Integer smartlistId;
@OneToOne( mappedBy = "smartlist", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, orphanRemoval = true)
private AccountEmailing accountEmailing;
}
@Entity
@Table(name = "account_emailing")
public class AccountEmailing implements Serializable {
@Id
@OneToOne
@JoinColumn(name = "smartlistId")
private Smartlist smartlist;
}
使用这些修改后的实体。您需要@MapsId 以及智能列表实体中的 setter。
@Entity
public class Smartlist {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "smartlistId")
private Integer smartlistId;
@OneToOne( mappedBy = "smartlist", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private AccountEmailing accountEmailing;
String name;
public Integer getSmartlistId() {
return smartlistId;
}
public void setSmartlistId(Integer smartlistId) {
this.smartlistId = smartlistId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addAccountEmail(AccountEmailing emailing)
{
accountEmailing=emailing;
accountEmailing.setSmartlist(this);
}
}
@Entity
@Table(name = "account_emailing")
public class AccountEmailing implements Serializable {
@Id
@Column(name="smartlistId")
Integer id;
@MapsId
@OneToOne
@JoinColumn(name = "smartlistId")
private Smartlist smartlist;
String name;
public Smartlist getSmartlist() {
return smartlist;
}
public void setSmartlist(Smartlist smartlist) {
this.smartlist = smartlist;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
使用以下代码关联实体
Smartlist smartlist=new Smartlist();
smartlist.setName("SmartList");
AccountEmailing accountEmailing=new AccountEmailing();
accountEmailing.setName("AccountEmailing");
smartlist.addAccountEmail(accountEmailing);
smartListRepo.saveAndFlush(smartlist);
更新时,我们必须从父对象中获取引用,否则它将无法工作,因为每次都会创建一个新对象
因此,对于上面的插入很好,对于更新以下需要应用
Smartlist smartlist = smartlistRepository.findOne(smartlistDTO.getSmartlistId());
// take an existence reference
AccountEmailing accountEmailing = smartlist.getAccountEmailing();
// perform update on accountEmailing
smartlist.setAccountEmailing(accountEmailing);
smartlistRepository.save(smartlist);
@MapsId 注解告诉 Hibernate 使用父实体的主键值作为子实体的主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.