[英]Hibernate - Join Table with Composite ID
I have a Person Table ( PersonID
), and an Address table ( AddressID
). 我有一个人表(
PersonID
)和一个地址表( AddressID
)。 I want to create a new Join table called Person_Address
which maps Person->Adress
using a ManyToMany
relationship and I have to specify the primary key as a composition of the two primary keys ( PersonID - AddressID
). 我想创建一个名为
Person_Address
的新Join表,该表使用ManyToMany
关系映射Person_Address
Person->Adress
并且必须指定主键作为两个主键( PersonID - AddressID
)的组合。
How can I do that using Hibernate HBM syntax? 如何使用Hibernate HBM语法做到这一点?
Use @ManyToMany
and forget about join tables in Hibernate. 使用
@ManyToMany
, @ManyToMany
Hibernate中的@ManyToMany
表。 The join table will exist in your database, but not in your code, Hibernate knows how to handle this. 连接表将存在于您的数据库中,但不在您的代码中,Hibernate知道如何处理。
Example: 例:
Person class (as the owner of the relationship): 人员类(作为关系的所有者):
@ManyToMany(targetEntity = Address.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = fetchType.LAZY)
@JoinTable(name = "person_address", joinColumns = @JoinColumn(name = "PersonID"), inverseJoinColumns = @JoinColumn(name = "AddressID"))
List<Address> addresses;
Address class (if it's required): 地址类别(如果需要):
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "addresses", targetEntity = Person.class, fetch = FetchType.LAZY)
List<Person> persons;
Edit: 编辑:
This link will help you with the many-to-many XML mapping. 该链接将帮助您进行多对多XML映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.