我得到了:
org.hibernate.MappingException: Foreign key (FKBB979BF4266AA123:address [a_id]))
must have same number of columns as the referenced
primary key (address [p_id,a_id])
当我尝试运行以下(虽然不完整)代码段
时 public static void main(String args[]) {
Configuration config = new Configuration().configure();
SessionFactory sessFact = config.buildSessionFactory();
Session sess = sessFact.openSession();
Transaction trans = sess.beginTransaction();
}
hibernate mapping xml
如下所示:
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
<class name="pojo.Address" table="address">
<id column="a_id" name="addressID">
<generator class="foreign" />
</id>
<property name="address" column="address" />
</class>
我正在尝试many to many
和Person
类之间的Address
关联。
这种例外的原因是什么?
我使用这些sql命令创建了两个表person
和address
:
CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,address TEXT);
POJO
人
public class Person {
private int personID;
private String personName;
private Set addressSet;
public int getPersonID() {
return personID;
}
public void setPersonID(int personID) {
this.personID = personID;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Set getAddressSet() {
return addressSet;
}
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
地址
public class Address {
private int addressID;
private String address;
private Set personSet;
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 Set getPersonSet() {
return personSet;
}
public void setPersonSet(Set personSet) {
this.personSet = personSet;
}
}
答案 0 :(得分:1)
对于ManyToMany Relationshhip,您需要一个专用的映射表
6.2.4. Collections of values and many-to-many associations
即。你需要像PersonAddress Table那样的东西
CREATE TABLE personaddress (p_id integer, a_id integer)
其中p_id是人员表的FK参考,a_id是地址表的FK参考
答案 1 :(得分:1)
您需要为多对多关联指定不同的表名,因为它由一个单独的表处理:
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="person_address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
请注意,<set>
现在引用了person_addresses
表。使用默认配置,Hibernate可以自动创建它。
我看到了另一个错误:地址实体的ID生成器不应该是外来的,它通常用于一对一的关系(使用另一个关联对象的ID)。您可以使用相同的&#39;增量&#39;用于Person实体:
<class name="Address" table="address">
<id column="a_id" name="addressID">
<generator class="increment" />
</id>
<property name="address" column="address" />
</class>
答案 2 :(得分:0)
您需要创建一个参考表:
CREATE TABLE PersonsAddresses (personId BIGINT, addressId BIGINT)
并将集合的映射更改为:
<set name="addressSet" table="PersonsAddresses" order-by="personId">
<key column="personId" foreign-key="p_id"/>
<many-to-many column="addressId" node="a_id" class="pojo.Address" />
</set>