[英]Hibernate getting NULL in foreign key @oneToone mapping
I have a relation between two class like this:我在两个类之间有这样的关系:
Smartlist -> smartlistId` is PK Smartlist -> smartlistId` 是 PK
AccountEmailing -> smartlistId FK,PK AccountEmailing -> smartlistId FK,PK
AccountEmailing table might not have an initial reference record but later it could have a record AccountEmailing 表可能没有初始参考记录,但后来可能有记录
I am only using smartlist table repository我只使用 smartlist 表存储库
I had tried cascade.ALL
but getting null in FK table id我曾尝试过
cascade.ALL
但在FK表ID中获得空值
I have tried the following code which is working with the following combination,我已经尝试了以下代码,它使用以下组合,
initial data with
smartlist
andAccountEmailing
与初始数据
smartlist
和AccountEmailing
This is working I am getting a record in both the tables but later updating a record giving me an error as AccountEmailing already has an entry ( CascadeType.PERSIST
only allows insert not update for child)这是有效的,我在两个表中都有一条记录,但后来更新了一条记录,给我一个错误,因为 AccountEmailing 已经有一个条目(
CascadeType.PERSIST
只允许插入而不是更新子项)
initial data with smartlist and no data in AccountEmailing
带有智能列表的初始数据,而 AccountEmailing 中没有数据
means it will not create an entry in AccountEmailing but later adding a record it creating an insert statement for AccountEmailing and from next time it always updates意味着它不会在 AccountEmailing 中创建一个条目,但稍后会添加一条记录,它会为 AccountEmailing 创建一个插入语句,并且从下次它总是更新
I am looking for a solution that how can I update my class so I can perform CRUD on 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;
}
Use these modified entities.You need @MapsId along with a setter in the smartlist entity.使用这些修改后的实体。您需要@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;
}
}
Use the following code to associate the entities使用以下代码关联实体
Smartlist smartlist=new Smartlist();
smartlist.setName("SmartList");
AccountEmailing accountEmailing=new AccountEmailing();
accountEmailing.setName("AccountEmailing");
smartlist.addAccountEmail(accountEmailing);
smartListRepo.saveAndFlush(smartlist);
when updating we have to took reference from the parent object otherwise it will not work as it will going to create a new object each time更新时,我们必须从父对象中获取引用,否则它将无法工作,因为每次都会创建一个新对象
so, For insert above is fine, for update following need to apply因此,对于上面的插入很好,对于更新以下需要应用
Smartlist smartlist = smartlistRepository.findOne(smartlistDTO.getSmartlistId());
// take an existence reference
AccountEmailing accountEmailing = smartlist.getAccountEmailing();
// perform update on accountEmailing
smartlist.setAccountEmailing(accountEmailing);
smartlistRepository.save(smartlist);
The @MapsId annotation tells Hibernate to use the primary key value of parent entity as primary key of child entity. @MapsId 注解告诉 Hibernate 使用父实体的主键值作为子实体的主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.