这是我目前的情况: 我有diagram这样的话。在中间表中,我还有1个额外的列。这里的问题在表“Period_Area_CC”中,列“Area_CompNo”由2列引用(表“CC”中的“CompNo”和来自tablle“Period_Area”的“Area_CompNo”)
这是我的映射
public class Area extends BaseEntity {
@EmbeddedId
private AreaId id;
@OneToMany(mappedBy = "area", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AreaPeriod> areaPeriods;
}
public class AreaPeriod implements Serializable {
@Id
@Column(name = "periodAssignmentID")
private Long periodID;
@Id
@Column(name = "areaID")
private Long areaID;
@Id
@Column(name = "companyNo")
private Long companyNo;
private Long parentId;
@Column(name = "hierarchyPath")
private String hierarchyPath;
@ManyToOne
@JoinColumns({@JoinColumn(name = "areaID", referencedColumnName = "areaID", updatable = false, insertable = false),
@JoinColumn(name = "companyNo", referencedColumnName = "companyNo", updatable = false, insertable = false)})
private Area area;
@ManyToOne
@JoinColumns({@JoinColumn(name = "periodAssignmentID", referencedColumnName = "periodAssignmentID", updatable = false, insertable = false)})
private VirtualAreaPeriodDefinition period;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private Set<AreaPeriod> children;
@ManyToOne
@JoinColumns({@JoinColumn(name = "periodAssignmentID", referencedColumnName = "periodAssignmentID", insertable = false, updatable = false),
@JoinColumn(name = "parentId", referencedColumnName = "areaID", insertable = false, updatable = false),
@JoinColumn(name = "companyNo", referencedColumnName = "companyNo", insertable = false, updatable = false)})
private AreaPeriod parent;
@OneToMany(mappedBy = "areaPeriod", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CostCenterAreaPeriod> costCenterAreaPeriods;
}
public class CostCenterAreaPeriod implements Serializable {
@Id
@Column(name = "companyNo")
private Long companyNo;
@Id
@Column(name = "periodAssignmentID")
private Long periodId;
@Id
@Column(name = "areaID")
private Long areaId;
@Id
@Column(name = "CostCenterEnum")
private Long costCenterEnum;
private String hierarchyPath;
@ManyToOne
@JoinColumns({@JoinColumn(name = "areaID", referencedColumnName = "areaID", updatable = false, insertable = false),
@JoinColumn(name = "companyNo", referencedColumnName = "companyNo", updatable = false, insertable = false),
@JoinColumn(name = "periodAssignmentID", referencedColumnName = "periodAssignmentID", updatable = false, insertable = false)})
private AreaPeriod areaPeriod;
@ManyToOne
@JoinColumns({@JoinColumn(name = "CompanyNo", referencedColumnName = "companyNo", updatable = false, insertable = false),
@JoinColumn(name = "CostCenterEnum", referencedColumnName = "CostCenterEnum", updatable = false, insertable = false)})
private CostCenter costCenter;
}
public class CostCenter extends BaseEntity {
private static final long serialVersionUID = -2290076530189347245L;
@EmbeddedId
private CXCostCenterPK id;
@OneToMany(mappedBy = "costCenter", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CostCenterAreaPeriod> costCenterAreaPeriods;
}
因为我已经标记了与insertable和updateable false的关系所以我必须手动插入。 那么有没有更好的解决方案,所以我只需要在实体上映射关系,然后JPA将处理插入/更新/删除功能?
更新:
public class CXCostCenterPK implements Serializable {
private static final long serialVersionUID = 658550094878047277L;
@Column(name = "CompanyNo")
private Long companyNo;
@Column(name = "CostCenterEnum")
private Long costCenterEnum;
}
public class AreaId implements Serializable {
@Column(name = "companyNo", length = 5, nullable = false)
private Long companyNo;
@Column(name = "areaID", length = 11, nullable = false)
private Long areaEnum;
}