为什么会出现“映射异常”?
我正进入(状态 :
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
发生此异常的原因是什么?
我创建了两个表person
并address
使用以下sql命令:
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;
}
}
回答:
对于多对多关系交换,您需要专用的映射表
6.2.4。价值和多对多关联的集合
即,您需要类似PersonAddress表的内容
CREATE TABLE personaddress (p_id integer, a_id integer)
其中p_id是对人员表的FK引用,而a_id是对地址表的FK引用
以上是 为什么会出现“映射异常”? 的全部内容, 来源链接: utcz.com/qa/404394.html