爪哇一角

共同探讨STRUTS#HIBERNATE#SPRING#EJB等技术
posts - 3, comments - 6, trackbacks - 0, articles - 99
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Hibernate中实现one-to-many

Posted on 2006-09-20 10:04 非洲小白脸 阅读(372) 评论(0)  编辑  收藏 所属分类: hibernate

Hibernate中实现one-to-many

近两天来在研究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();
        }
    }
}


只有注册用户登录后才能发表评论。


网站导航: