2014-01-08 31 views
2

我在將元素插入由地圖表示的@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

回答

1

只需再次測試,我發現,這個問題是獻給事務管理。我根本不想使用交易,因此我用@TransactionManagement(TransactionManagementType.BEAN)註釋了我的SLSB。 當我刪除它時,事務被容器管理,然後更新按預期工作。

將事務管理設置爲託管容器不是我希望的解決方案,但現在可以使用。不過我認爲這是eclipselink中的一個錯誤。

相關問題