我在將元素插入由地圖表示的@ElementCollection
中存在問題。當插入具有相同值的元素時,它們不會被保留。 鑑於以下@Entity
及其@Embeddable
:@ElementCollection當字段值相等時,@Embeddable的映射不會持久
@Entity
@Table(name = "category", catalog = "my_db", schema = "")
public class Category implements Serializable {
@Id
@Column(name = "id")
private Integer id;
@ElementCollection(fetch = FetchType.LAZY, targetClass = CategoryLabels.class)
@CollectionTable(name = "category_labels", joinColumns =
@JoinColumn(name = "category_id"), catalog = "my_db")
@MapKeyColumn(name = "language_id")
private Map<Integer, CategoryLabels> labels = new HashMap<Integer, CategoryLabels>();
...
}
@Embeddable
public class CategoryLabels implements Serializable {
@Column(name = "label1")
private String label1;
@Column(name = "label2")
private String label2;
...
}
數據庫條目
+-------------+-------------+-----------+---------+
| category_id | language_id | label1 | label2 |
+-------------+-------------+-----------+---------+
| 183 | 1 | Capacity | Timings |
| 183 | 2 | | |
+-------------+-------------+-----------+---------+
插入工作正常,如果label1和label2每個映射項不同(如上所示)。
但是,如果我添加一個條目與另一個條目具有相同的label1和label2值,則第二個條目是永不會將保留到數據庫。 例如,如果我向兩個標籤均爲空的Map添加條目(如示例中的第二個條目),但是language_id = 3,則永遠不會持久化。
我知道這個共同的問題:http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection#Common_Problems和檢查沒有ID的嵌入對象的字段和外鍵@JoinColumns
作爲ID。 這似乎不適用於我的情況,@JoinColumn
中的外鍵在ID檢查中完全被忽略。
這是Eclipselink中的錯誤還是我做錯了什麼?
編輯:
我設置-Declipselink.logging.level=FINEST
,這表明沒有SQLS正在與空標籤,第二次及以後的條目執行。
此外,我所做的,使用它可以重現該問題的示例項目: http://bit.ly/1bR8ywO