2011-11-26 89 views
1

我有包含兩個元件的集合作爲這樣的實體對象:如何創建包含2個元素集合的實體?

@Entity 
public class Report { 

    // Electronic transactions items map 
    @ElementCollection(fetch = FetchType.EAGER) 
    @MapKeyColumn(name = "catElecItem_key", nullable = false) 
    @Cascade(value = { CascadeType.ALL }) 
    public Map<Category, ReportItem> catElecItemMap; 

    // Branch transactions items map 
    @ElementCollection(fetch = FetchType.EAGER) 
    @MapKeyColumn(name = "catBranchItem_key", nullable = false) 
    @Cascade(value = { CascadeType.ALL }) 
    public Map<Category, ReportItem> catBranchItemMap; 
} 

當正在創建的表,一個表稱爲Report_ReportItem正在生成做類別和映射的之間的映射ReportItem。然而,當試圖保存地圖時失敗,enther * catElecItem_key *或* catBranchItem_key *將爲空。 如果我嘗試使用nullable = true進行註釋,表格將無法創建,因爲兩個鍵都用作主鍵定義的一部分。

是否可以指定每個集合使用不同的表?

回答

4

您是否嘗試過使用@CollectionTable註釋?

@ElementCollection(fetch = FetchType.EAGER) 
@CollectionTable(name="<custom_map_table_name>") 
@MapKeyColumn(name = "catElecItem_key", nullable = false) 
@Cascade(value = { CascadeType.ALL }) 
public Map<Category, ReportItem> catElecItemMap; 
0

通過使用@CollectionTable註釋解決了問題。新班級現在看起來像:

@Entity 
public class Report { 
    // Electronic transactions items map 
    @ElementCollection(fetch = FetchType.EAGER) 
    @MapKeyColumn(name = "catElecItem_key", nullable = false) 
    @CollectionTable(name = "TrxReportElecItems", joinColumns = @JoinColumn(name = "id")) 
    @Cascade(value = { CascadeType.ALL }) 
    public Map<Category, ReportItem> catElecItemMap; 

    // Branch transactions items map 
    @ElementCollection(fetch = FetchType.EAGER) 
    @MapKeyColumn(name = "catBranchItem_key", nullable = false) 
    @CollectionTable(name = "TrxReportBranchItems", joinColumns = @JoinColumn(name = "id")) 
    @Cascade(value = { CascadeType.ALL }) 
    public Map<Category, ReportItem> catBranchItemMap; 
    ... 
} 
相關問題