我有2个表如下:
TABLE_A
-------
A_SIREN
A_NDA
TABLE_B
-------
B_id
B_NDA
B_SIREN
表A的ID是一个COMPOSITE KEY
SIREN / NDA
下面是实体代码。
重点班
@Embeddable
public class SirenNdaKey implements Serializable {
@Column(name = "A_SIREN")
protected String siren;
@Column(name = "A_NDA")
protected String nda;
// getters setters
}
表A
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;
...
}
表B
public class EntityB
@Id
private long id;
@Column(name = "B_SIREN")
private String siren;
@Column(name = "B_NDA")
private String nda;
}
Hibernate来生成3个表:TABLE_A,表-B和关联表包含A_NDA,A_SIREN,B_ID。我该怎么办只产生2个表。
我OBJECTIF是要找到关联到同一对夫妇SIREN / NDA从实体A(entityA.getEntityBSet()
),而不需要关联表的entityB的名单,因为我的表是由外部批次供应。
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;
...
}