[英]Nullable Map mapping in jpa
我正在嘗試使用JPA2.1和Hibernate 4.3.7將帶有實體值映射的實體鍵映射到數據庫。 這是我的代碼:
@Entity
@Audited
class Form{
//id
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "form_content",
joinColumns = @JoinColumn(name = "id_form", nullable = false),
inverseJoinColumns = @JoinColumn(name = "id_field_content"/*non-working part start*/, unique = false, nullable = true/*non-working part stop*/),
uniqueConstraints = { @UniqueConstraint(columnNames = { "id_field", "id_form" }) })
@MapKeyJoinColumn(name = "id_field", nullable = false)
private Map<FormEntryDefinition, FormEntryValue> content;
//getters, setters, equals, etc.
}
Hibernate生成表
form_content(
id_form bigint primary key not null,
id_field_content bigint <!-- problem start -->not null unique <!--problem stop -->,
id_field bigint primary key not null)
在所有3個字段中使用正確的外鍵。
誰能告訴我,為什么休眠會生成唯一的約束而不是非null約束,所以我無法使用可為null的值持久化map?
有沒有解決該問題的方法?
唯一約束是由於使用@OneToMany
關聯。 如果將其更改為@ManyToMany
則不再需要唯一約束。
聯接表FK列僅對不可為空的列有意義。 聯接表中的一行是兩個表之間的鏈接,如果缺少一個FK,則關聯將中斷,這等效於首先沒有鏈接行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.