为什么我得到映射异常? [英] Why am I getting Mapping Exception?

查看:115
本文介绍了为什么我得到映射异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  org.hibernate.MappingException:外键(FKBB979BF4266AA123:address [a_id]))
必须与引用的
主键(地址[p_id,a_id])


$ b $具有相同的列数b

当我尝试运行以下(尽管不完整)片段:

  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.Persontable =person> 
< id column =p_idname =personID>
< generator class =increment/>
< / id>
< property name =personNamecolumn =p_name/>
< key column =p_id/>
< / set>
< / class>

< class name =pojo.Addresstable =address>
< id column =a_idname =addressID>
< generator class =foreign/>
< / id>
< property name =addresscolumn =address/>
< / class>

我正在尝试多对多关联介于 Person Address 类之间。



这个异常的原因是什么?



我创建了两个表 person 地址使用这些sql命令:

  CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id)); 
CREATE TABLE地址(a_id INTEGER,address TEXT);

POJO



Person

  public class Person {
private int personID;
私人字符串personName;
private设置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;
私有字符串地址;
私人设置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;
}

}


解决方案

对于一个ManyToMany关系,您需要一个专门的映射表

6.2.4。价值集合和多对多关联



即你需要一个像PersonAddress Table

  CREATE TABLE personaddress(p_id integer,a_id integer)

其中p_id是FK对Person Table的引用,a_id是FK对Address Table的引用

I am getting :

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])

as I try to run the following (though incomplete) snippet :

    public static void main(String args[]) {
        Configuration config = new Configuration().configure();
        SessionFactory sessFact = config.buildSessionFactory();
        Session sess = sessFact.openSession();
        Transaction trans = sess.beginTransaction();    
    }

The hibernate mapping xml is shown below :

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

I am trying a many to many association between Person and Address class.

What is the reason for this exception ?

I have created two tables person and address using these sql commands :

CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,address TEXT);

POJO

Person

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

Address

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

}

解决方案

For a ManyToMany Relationshhip you need a dedicated mapping table

6.2.4. Collections of values and many-to-many associations

i.e. You need something like a PersonAddress Table

CREATE TABLE personaddress (p_id integer, a_id integer)

Where p_id is a FK Reference to the Person Table and a_id a FK Reference to the Address Table

这篇关于为什么我得到映射异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆