[英]JPA and Hibernate One To One Shared Primary Key Uni-directional Mapping in Spring Boot
我想使用共享主鍵與 2 個子實體進行一對一的單向映射。 以下是 model 類
public class Template implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "templatekey")
Integer templateKey;
@Column(name = "templateid", unique = true)
String templateId;
@OneToOne(cascade = CascadeType.ALL, optional = false)
@PrimaryKeyJoinColumn(name = "templatekey", referencedColumnName = "templatekey")
InstantOfferNoEsp instantOfferNoEsp;
@OneToOne(cascade = CascadeType.ALL, optional = false)
@PrimaryKeyJoinColumn(name = "templatekey", referencedColumnName = "templatekey")
Mobile mobile;
//constructor , setter and getters
}
Child 1 :
public class Mobile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "templatekey")
Integer templateKey;
String mobileNewUrl;
//constructor , setter and getters
}
Child 2:
public class InstantOfferNoEsp {
@Id
@Column(name = "templatekey")
Integer templateKey;
String offerCodeType;
String headerUrl;
//constructor , setter and getters
}
我希望 templateKey 在所有表中都作為 PK。 我正在調用templateRepository.save(template);
一次保存所有實體,但它無法正常工作,並且ids for this class must be manually assigned before calling save()
。
任何建議都會有很大幫助。 謝謝你。
我可以使用雙向@OneToOne
做你想做的事情,如下所示:
@Entity
public class Mobile {
@Id
Integer templateKey;
@OneToOne
@MapsId
@JoinColumn(name = "templatekey")
Template template;
// ...
}
@Entity
public class InstantOfferNoEsp {
@Id
Integer templateKey;
@OneToOne
@MapsId
@JoinColumn(name = "templatekey")
Template template;
// ...
}
@Entity
public class Template {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "templatekey")
Integer templateKey;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "template", optional = false)
InstantOfferNoEsp instantOfferNoEsp;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "template", optional = false)
Mobile mobile;
// ...
public void setMobile(Mobile mobile)
{
this.mobile = mobile;
this.mobile.setTemplate(this);
}
public void setInstantOfferNoEsp(InstantOfferNoEsp instantOfferNoEsp)
{
this.instantOfferNoEsp = instantOfferNoEsp;
this.instantOfferNoEsp.setTemplate(this);
}
}
和一個保存的例子:
Mobile mobile = new Mobile();
mobile.setMobileNewUrl("MOB1");
InstantOfferNoEsp instant = new InstantOfferNoEsp();
instant.setOfferCodeType("INST_OFF1");
Template template = new Template();
template.setTemplateId("TMP1");
template.setInstantOffer(instant);
template.setMobile(mobile);
entityManager.persist(template);
PS 以下映射也有效,但前提是我們手動設置Template.templateKey
。
@Entity
public class Template
{
@Id
// @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "templatekey")
Integer templateKey;
@OneToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "templatekey", insertable = false, updatable = false)
InstantOfferNoEsp instantOfferNoEsp;
@OneToOne(cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "templatekey", insertable = false, updatable = false)
Mobile mobile;
// ...
}
和一個保存的例子:
Mobile mobile = new Mobile();
mobile.setMobileNewUrl("MOB1");
InstantOfferNoEsp instant = new InstantOfferNoEsp();
instant.setOfferCodeType("INST_OFF1");
Template template = new Template();
template.setTemplateKey(20);
template.setTemplateId("TMP1");
template.setInstantOffer(instant);
template.setMobile(mobile);
entityManager.persist(template);
另外,我建議您明確指定要使用的生成策略(不要使用GenerationType.AUTO
)並使用相應的 object 包裝類而不是@Id
字段的原始類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.