为什么我会获得映射异常?

时间:2013-06-25 07:39:58

标签: java hibernate java-ee hibernate-mapping

我得到了:

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 manyPerson类之间的Address关联。

这种例外的原因是什么?

我使用这些sql命令创建了两个表personaddress

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;
    }

}

3 个答案:

答案 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>