簡體   English   中英

如何創建具有2個元素集合的實體?

[英]how can I create entity with 2 element collections?

我有一個實體對象,其中包含兩個元素集合:

@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的表來執行Category和映射的ReportItem之間的映射。 但是,當嘗試保存地圖時,這會失敗,enther * catElecItem_key *或* catBranchItem_key *將為null。 如果我嘗試使用nullable = true進行批注,則無法創建表,因為兩個密鑰都用作主鍵定義的一部分。

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

您是否嘗試過使用@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;

使用@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;
   ... 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM