Hibernate中的one-to-many

用一个例子来说明Hibernate中的one-to-many的配置,包含一个Parent(one)和一个Child(many).代码如下:由于使用的是Struts+Hibernate进行试验,所以Parent继承了ActionForm类。

Parent.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

    <class name="com.ln.hb.Parent" table="parent" >

        <id name="pid" column="pid" type="integer">

            <generator class="identity" />

        </id>

        <property name="pname" column="pname" />

        <set name="child" lazy="true" inverse="true" cascade="all">

            <key column="pid" />

设置子表外键,使得Hibernate可以根据查询结果的主键值与子表外键进行对照,查出所需要的子结果。子表中必须存在这一字段且类型要与主键一致。

            <one-to-many class="com.ln.hb.Child"/>

        </set>

    </class>

</hibernate-mapping>

 

Parent.java

package com.ln.hb;

 

import java.util.HashSet;

 

import org.apache.struts.action.ActionForm;

 

public class Parent extends ActionForm {

              private String pname;

              private java.util.Set child = new HashSet();

              private int pid;

 

              public Parent() {

              }

              public int getPid() {

                            return pid;

              }

              public void setPid(int 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;

              }

}

Child.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

    <class name="com.ln.hb.Child" table="child">

        <id name="cid" column="cid" >

            <generator class="identity"/>

        </id>

        <property name="cname" column="cname" />

        <many-to-one name="parent" column="pid" />

设置父表外键,使得Hibernate可以根据查询结果的子表外键值与父表的外键值进行对照,查出所需要的父结果。父表中必须存在这一字段且类型要与主键一致。

    </class>

</hibernate-mapping>

Child.java

package com.ln.hb;

 

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;

              }

}

 

TestAction.java

 

package com.ln.struts.action;

 

import java.util.Iterator;

import java.util.Set;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import net.sf.hibernate.Query;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.Transaction;

import net.sf.hibernate.cfg.Configuration;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

 

import com.ln.hb.Child;

import com.ln.hb.Parent;

 

public class TestAction extends Action {

 

              public ActionForward execute(ActionMapping mapping, ActionForm form,

                                          HttpServletRequest request, HttpServletResponse response) throws Exception {

                            Parent p= (Parent)form;

                            Child c=new Child();

                            SessionFactory sf = new Configuration().configure()

                                                        .buildSessionFactory();

                            Session session = sf.openSession();

                            Transaction tx = session.beginTransaction();

                            System.out.println("Let's go!");

                            Query query = session

                                                        .createQuery("select p from Parent as p where PID=:pid");

                            query.setString("pid","1");

                            for (Iterator it = query.iterate(); it.hasNext();) {

                                          Parent p1=(Parent) it.next();

                                          p.setPname(p1.getPname().toString());

                                          System.out.println(p.getPname());

                                          Set set=p1.getChild();

                                          for(Iterator ic=set.iterator();ic.hasNext();)

                                          {

                                                        c=(Child) ic.next();

                                                        System.out.println(c.getCname());

                                          }

                            }

                            tx.commit();

                            session.close();

                            return mapping.findForward("main");

              }

 

}

注意:

1.类中声明的变量的类型要与数据库中类型相一致,所声明的字段在数据库中也要有相应的字段与之对应。

posted @ 2009-05-14 14:22  QQ天堂  阅读(2525)  评论(0编辑  收藏  举报