Hibernate中实现one-to-many

类别:Java 点击:0 评论:0 推荐:

近两天来在研究Hibernate中的one-to-one和one-to-many的问题,做了很多试验,发现实现表关系时,在hibernate-mapping中generator的class设为uuid.hex,上述的两种关系可以实现,也就是数据库的主键设为varchar(MSSQL2000),但是主键改成int型的标识却弹出“ERROR SessionImpl:2379 - Could not synchronize database state with session”和“net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)”异常,百思不得其解。下面贴出主键设为uuid.hex时的实现代码:

Child.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
    <class name="test.pojo.Child" table="child">
        <id name="cid" column="cid" >
            <generator class="uuid.hex"/>
        </id>
        <property name="cname" column="cname" />
        <many-to-one name="parent" column="pid" />
    </class>
</hibernate-mapping>

Child.java

package test.pojo;

public class Child {
    private String cname;
    private String pid;
    private Parent parent;
    private String cid;
    public Child() {
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }
}

Parent.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
    <class name="test.pojo.Parent" table="parent" >
        <id name="pid" column="pid" type="integer">
            <generator class="uuid.hex" />
        </id>
        <property name="pname" column="pname" />
        <set name="child" lazy="true" inverse="true" cascade="all">
            <key column="cid" />
            <one-to-many class="capinfo.negroup.test.pojo.Child"/>
        </set>
    </class>
</hibernate-mapping>

Parent.java
package test.pojo;

import java.util.HashSet;

public class Parent {
    private String pname;
    private java.util.Set child = new HashSet();
    private String pid;
    public Parent() {
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public java.util.Set getChild() {
        return child;
    }

    public void setChild(java.util.Set child) {
        this.child = child;
    }

}

TestHibernate.java
package test.pojo;

public class TestHibernate {
    public TestHibernate() {
    }

    public static void main(String[] argv) {
        //先建立多的初始数据
        Child child = new Child();
        child.setCname("kerry");
        //再建立一的初始数据
        Parent parent = new Parent();
        parent.setPname("Kelphen");
        //将多的数据放入到一里
        parent.getChild().add(child);
        //再将一放到多里,这样一与多互相拥有对方
        child.setParent(parent);
        try {
            BaseDAOHibernate.saveObject(parent);
        }
        catch (DAOException daoe) {
            daoe.printStackTrace();
        }
    }
}

本文地址:http://com.8s8s.com/it/it12259.htm