如何在2个 many-to-many
和 Person
类之间建立Address
关系?以下是Person
和Address
的 POJO 。
人:
public class Person {
private int personID;
private String name;
private Person person;
private List addressList;
public List getAddressList() {
return addressList;
}
public void setAddressList(List addressList) {
this.addressList = addressList;
}
public int getPersonID() {
return personID;
}
public void setPersonID(int personID) {
this.personID = personID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
地址:
public class Address {
private int addressID;
private String address;
private Person person;
public int getAddressID() {
return addressID;
}
public void setAddressID(int addressID) {
this.addressID = addressID;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
我想要定义的关系是A person can have many addresses and one address can belong to many people
。
我无法在mapping xml
中表示这一点。这是我迄今为止所能做的。
<hibernate-mapping>
<class name="pojo.Person" table="person">
<id name="personID" column="p_id">
<generator class="increment" />
</id>
<property name="name" column="p_name" />
<list name="addressList" table="address">
<key column="a_id" />
<!-- DEFINE MANY TO MANY --!>
</list>
</class>
<class name="pojo.Address" table="address">
<id name="addressID" column="a_id">
<generator class="" />
</id>
<property name="address" />
</class>
</hibernate-mapping>
如何定义这两个类之间的many to many
关系?
答案 0 :(得分:0)
在private List persons
课程中添加private Person person
而不是Address
。
像这样配置您的mapping.xml
//For person.hbm.xml
<list name="addresses" inverse="false" table="person_address">
<key>
<column name="Person_ID" not-null="true" />
</key>
<many-to-many entity-name="domains.Address">
<column name="listAddress_ID" not-null="true" />
</many-to-many>
</list>
//For address.hbm.xml
<list name="persons" inverse="false" table="person_address">
<key>
<column name="listAddress_ID" not-null="true" />
</key>
<many-to-many entity-name="domains.Person">
<column name="Person_ID" not-null="true" />
</many-to-many>
</list>