【发布时间】:2014-06-24 11:51:43
【问题描述】:
在我的项目中,我想在 mappedBy 属性中使用具有多个键的 @OneToMany 关系。
在我的项目中,我有 IncidentReport 实体和 ReportEntry 实体,下面的映射很简单
@Entity
public class ReportEntry {
...
@JoinColumn(name = "incident_report", referencedColumnName = "id")
@ManyToOne
private IncidentReport incidentReport;
private int type; // 0,1,2
...
}
@Entity
public class IncidentReport {
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "incidentReport")
private Collection<ReportEntry> reportEntryCollection;
...
}
但理想情况下,我希望在 IncidentReport 类中有 3 个不同的 ReportEntry 集合(每种类型的报告条目一个)。
有没有办法根据 2 个键映射集合,比如 incidentReport 和 type 的值?
类似这样的:
@Entity
public class IncidentReport {
...
// Type 0
@OneToMany(cascade = CascadeType.ALL, mappedBy = "incidentReport",
mappedBy = "type = 0" )
private Collection<ReportEntry> reportEntryCollection1;
// Type 1
@OneToMany(cascade = CascadeType.ALL, mappedBy = "incidentReport"
mappedBy = "type = 1" )
private Collection<ReportEntry> reportEntryCollection1;
// Type 2
@OneToMany(cascade = CascadeType.ALL, mappedBy = "incidentReport"
mappedBy = "type = 2" )
private Collection<ReportEntry> reportEntryCollection1;
...
}
有没有办法做到这一点?我的后备计划是添加另一个实体来分别映射它们。哪种方法真的很糟糕?
【问题讨论】:
标签: java jakarta-ee jpa ejb ejb-3.0