jpa一对多复合主键映射
jpa one to many composite primary key mapping
我有 2 个 table 如下:
TABLE_A
-------
A_SIREN
A_NDA
TABLE_B
-------
B_id
B_NDA
B_SIREN
tableA的id是COMPOSITE KEY
SIREN/NDA
这是实体代码。
键class
@Embeddable
public class SirenNdaKey implements Serializable {
@Column(name = "A_SIREN")
protected String siren;
@Column(name = "A_NDA")
protected String nda;
// getters setters
}
TABLE一个
public class EntityA{
@EmbeddedId
private SirenNdaKey sirenNda;
@OneToMany(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name = "A_SIREN",
referencedColumnName = "B_SIREN"),
@PrimaryKeyJoinColumn(name = "A_NDA", referencedColumnName = "B_NDA")
})
private Set<EntityB> EntityBSet;
...
}
TABLE B
public class EntityB
@Id
private long id;
@Column(name = "B_SIREN")
private String siren;
@Column(name = "B_NDA")
private String nda;
}
Hibernate 生成 3 tables:TABLE_A、TABLE_B 和包含 A_NDA、A_SIREN、[=39 的关联 table =].我该怎么做才能只生成 2 tables.
我的目标是从实体 A (entityA.getEntityBSet()
) 中找到与同一对 SIREN/NDA 关联的实体 B 的列表,而不需要关联 table,因为我的 tables 由外部批次提供。
entityA = entityARepository.findOne(new SirenNdaKey("nda_test1", "siren_test1"));
entityA.getEntityBSet() // this list is always empty
这是正确的源代码,我应该使用 @JoinColumns
而不是 @PrimaryKeyJoinColumns
public class EntityA{
@EmbeddedId
private SirenNdaKey sirenNda;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "B_SIREN",
referencedColumnName = "A_SIREN"),
@JoinColumn(name = "B_NDA", referencedColumnName = "A_NDA")
})
private Set<EntityB> EntityBSet;
...
}
我有 2 个 table 如下:
TABLE_A
-------
A_SIREN
A_NDA
TABLE_B
-------
B_id
B_NDA
B_SIREN
tableA的id是COMPOSITE KEY
SIREN/NDA
这是实体代码。
键class
@Embeddable
public class SirenNdaKey implements Serializable {
@Column(name = "A_SIREN")
protected String siren;
@Column(name = "A_NDA")
protected String nda;
// getters setters
}
TABLE一个
public class EntityA{
@EmbeddedId
private SirenNdaKey sirenNda;
@OneToMany(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name = "A_SIREN",
referencedColumnName = "B_SIREN"),
@PrimaryKeyJoinColumn(name = "A_NDA", referencedColumnName = "B_NDA")
})
private Set<EntityB> EntityBSet;
...
}
TABLE B
public class EntityB
@Id
private long id;
@Column(name = "B_SIREN")
private String siren;
@Column(name = "B_NDA")
private String nda;
}
Hibernate 生成 3 tables:TABLE_A、TABLE_B 和包含 A_NDA、A_SIREN、[=39 的关联 table =].我该怎么做才能只生成 2 tables.
我的目标是从实体 A (entityA.getEntityBSet()
) 中找到与同一对 SIREN/NDA 关联的实体 B 的列表,而不需要关联 table,因为我的 tables 由外部批次提供。
entityA = entityARepository.findOne(new SirenNdaKey("nda_test1", "siren_test1"));
entityA.getEntityBSet() // this list is always empty
这是正确的源代码,我应该使用 @JoinColumns
而不是 @PrimaryKeyJoinColumns
public class EntityA{
@EmbeddedId
private SirenNdaKey sirenNda;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "B_SIREN",
referencedColumnName = "A_SIREN"),
@JoinColumn(name = "B_NDA", referencedColumnName = "A_NDA")
})
private Set<EntityB> EntityBSet;
...
}