![](/img/trans.png)
[英]JPA / Hibernate unidirectional one-to-one mapping with shared primary key
[英]Hibernate JPA one to one bidirectional mapping with shared primary key foreign key field is null (hb can't set it automatically)
所以我在最后一天試圖獲得一些單向的一對一映射,經過一些測試,我決定嘗試另一種方法。 當我試圖堅持我的實體時偶然發現了一個問題。 我找不到一種方法來強制 hibernate 同時來回填充我的外鍵字段和引用。 它只將它們設置為一種方式,而不是另一種方式:
Item ---> ItemInfo (I populate info field manually in constructor)
Item <-x- ItemInfo (hibernate doesn't set item field on persist)
因此,當我嘗試保留Item
實體時,我從@MapsId
得到異常: attempted to assign id from null one-to-one property [by.test.hm.ItemInfo.item]
我的桌子:
t_item_info(item_id)
引用t_item(id)
字段。 只有t_item(id)
列自動遞增
我的課程:
@Entity
@Table(name = "t_item")
class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "item", cascade = CascadeType.ALL,
fetch = FetchType.LAZY, orphanRemoval = true, optional = false)
private ItemInfo info;
private String name;
}
另一個:
@Entity
@Table(name = "t_item_info")
class ItemInfo {
@Id
private Long itemId;
@MapsId
@OneToOne
@JoinColumn(name = "item_id", referencedColumnName = "id")
private Item item;
private String descr;
}
我如何創建和持久化對象:
Item item = new Item(
null,
new ItemInfo(null, "Somde description"),
"Some name"
);
itemRepo.save(item);
例外:
org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [by.test.hm.ItemInfo.item] at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:87) at org .hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:115) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener .java:128) 在 org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.Z93F725A0742333FE1C889F4118)
我嘗試了很多東西。 在我聲明mappedBy從Item
到ItemInfo
的切換端,在這種情況下,一切都停止工作,因為ItemInfo
試圖首先得到持久化。 我嘗試了optional = false\true
和nullable = false\true
的所有可能組合,但似乎沒有任何效果 =(
我知道我可以手動設置引用,異常就會消失,我知道我可以使用@PostPersist
來設置它,但是當 hibernate 自動為我執行此操作時,我正在尋找答案
PS:我發布了關於單向映射的類似問題,我是新來的,所以我希望在為類似問題創建新問題時我沒有做錯任何事情
@Entity
@Table(name = "t_item")
class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "item", cascade = CascadeType.ALL,
fetch = FetchType.LAZY, orphanRemoval = true, optional=false)
private ItemInfo info;
@Column(name="name)
private String name;
// Getters and setters omitted for brevity
private void setItemInfo(ItemInfo info) {
if (info == null) {
if (this.info != null) {
this.info.setInfo(null);
}
} else {
info.setInfo(this);
}
this.info = info;
}
@Entity
@Table(name = "t_item_info")
class ItemInfo {
@Id
private Long itemId;
@OneToOne
@MapsId
@JoinColumn(name="item_id)
private Item item;
@Column(name="description")
private String description;
// Getters and setters omitted for brevity
}
超出主題,但您的課程名稱相同。
試試上面的代碼............
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.