Hibernate 将两个外键关系解释为复合主键(MappingException)。为什么?
Hibernate interprets two foreign key relations as composite primary key(MappingException). Why?
我有三个 类:A 与 B 具有 OneToMany 关系,B 与 C 具有 OneToMany 关系。我想在 Class A 中有一个 Map 包含 B_ID 作为键和 B 对象作为值。 B 应该在列表中包含 C 对象。使用 A_ID<>B_A_ID 和 B_RUN_ID<>C_RUN_ID 完成连接。
Class答:
class A{
@Id
@Column(name = "A_ID")
private Long aId;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "T_B",
schema = "owner",
joinColumns = @JoinColumn(name = "B_A_ID",
referencedColumnName = "A_ID",
nullable = false))
@MapKeyColumn(name = "B_RUN_ID")
@Column(name = "B_STATUS")
@Enumerated(javax.persistence.EnumType.STRING)
private Map<Long, Status> trackings;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "B_A_ID")
@MapKeyColumn(name = "B_ID")
private Map<Long,B> trackingB;
...}
Class乙:
class B{
@Id
@Column(name = "B_ID")
private Long bId;
@Column(name = "B_A_ID")
private A a;
@Column(name = "B_RUN_ID", unique=true)
private long runId;
@OneToMany(mappedBy = "c_runID")
private List<C> cTrackings;
...}
Class C:
class C{
@Id
@Column(name = "C_ID",unique=true)
private long id;
@ManyToOne
@JoinColumn(name = "B_RUN_ID")
private B c_runData;
@Column(name = "EX_ID")
private long externalId;
...}
我在部署时遇到以下错误:
MappingException:外键 "C[B_RUN_ID]" 必须与引用的主键具有相同的列数 "B[B_A_ID,B_RUN_ID]"
我的问题:为什么这些外键被解释为复合主键? B 的主键是生成的 "B_ID".
非常感谢任何帮助:)
Class A 有第二个可嵌入地图:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "T_B",
schema = "owner",
joinColumns = @JoinColumn(name = "B_A_ID",
referencedColumnName = "A_ID",
nullable = false))
@MapKeyColumn(name = "B_RUN_ID")
@Column(name = "B_STATUS")
@Enumerated(javax.persistence.EnumType.STRING)
private Map<Long, Status> trackings;
注释@MapKeyColumn(name = "B_RUN_ID") 似乎将 "B_RUN_ID" 定义为主键。
此外,正如我所研究的那样,不可能连接不是主键和外键的列,因此不可能通过 B_RUN_ID 进行连接。相反,我将外键列 "C_B_ID" 添加到 class C 并加入 "B_ID".
我有三个 类:A 与 B 具有 OneToMany 关系,B 与 C 具有 OneToMany 关系。我想在 Class A 中有一个 Map 包含 B_ID 作为键和 B 对象作为值。 B 应该在列表中包含 C 对象。使用 A_ID<>B_A_ID 和 B_RUN_ID<>C_RUN_ID 完成连接。
Class答:
class A{
@Id
@Column(name = "A_ID")
private Long aId;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "T_B",
schema = "owner",
joinColumns = @JoinColumn(name = "B_A_ID",
referencedColumnName = "A_ID",
nullable = false))
@MapKeyColumn(name = "B_RUN_ID")
@Column(name = "B_STATUS")
@Enumerated(javax.persistence.EnumType.STRING)
private Map<Long, Status> trackings;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "B_A_ID")
@MapKeyColumn(name = "B_ID")
private Map<Long,B> trackingB;
...}
Class乙:
class B{
@Id
@Column(name = "B_ID")
private Long bId;
@Column(name = "B_A_ID")
private A a;
@Column(name = "B_RUN_ID", unique=true)
private long runId;
@OneToMany(mappedBy = "c_runID")
private List<C> cTrackings;
...}
Class C:
class C{
@Id
@Column(name = "C_ID",unique=true)
private long id;
@ManyToOne
@JoinColumn(name = "B_RUN_ID")
private B c_runData;
@Column(name = "EX_ID")
private long externalId;
...}
我在部署时遇到以下错误:
MappingException:外键 "C[B_RUN_ID]" 必须与引用的主键具有相同的列数 "B[B_A_ID,B_RUN_ID]"
我的问题:为什么这些外键被解释为复合主键? B 的主键是生成的 "B_ID".
非常感谢任何帮助:)
Class A 有第二个可嵌入地图:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "T_B",
schema = "owner",
joinColumns = @JoinColumn(name = "B_A_ID",
referencedColumnName = "A_ID",
nullable = false))
@MapKeyColumn(name = "B_RUN_ID")
@Column(name = "B_STATUS")
@Enumerated(javax.persistence.EnumType.STRING)
private Map<Long, Status> trackings;
注释@MapKeyColumn(name = "B_RUN_ID") 似乎将 "B_RUN_ID" 定义为主键。
此外,正如我所研究的那样,不可能连接不是主键和外键的列,因此不可能通过 B_RUN_ID 进行连接。相反,我将外键列 "C_B_ID" 添加到 class C 并加入 "B_ID".