Hibernate中的one-to-many

原创 2005年03月04日 13:32:00

用一个例子来说明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.类中声明的变量的类型要与数据库中类型相一致,所声明的字段在数据库中也要有相应的字段与之对应。

Hibernate-one-to-many

  • 2011年12月15日 08:01
  • 98KB
  • 下载

Hibernate one-to-many-annotation

  • 2012年07月30日 15:13
  • 9KB
  • 下载

使用注解的Hibernate one-to-many映射

One to many映射关系指的是两个实体间一个实体可以和多个实体有关联关系,但是多的这一端只能和一的这一端的一个实例有关系。它是一个1 到 n的关系。例如在任何的公司员工可以注册多个银行账户,一个...

hibernate学习之——one-to-many(双向一对多关系)

hibernate单向一对多关联映射(one-to-many)XML与注解版

单向一对多关联映射原理:  hibernate对于一对多的关系的处理,是通过操作classess端,间接操作或者自动操作Student端。比如添加,我直接添加classess端的数据,多个S...

hibernate annotation one-to-many

hibernate一对多,多的一方建立外键,例: CREATE TABLE `person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `pe...

Hibernate的配置文件的映射many-to-one错误

先看一下错误 虽然,这么多错误挺吓人的,还有SessionFactory为空,但是其实就一个错误文件是这样的Customer.javapackage ch06.pojo;import jav...

Hibernate one-to-many example

1. “One-to-many” example This is a one-to-many relationship table design, a STOCK table has many oc...

hibernate:many to one

这里就拿练手的图书馆系统来说吧… 这个案例用到其中的表 tb_book 图书信息表 tb_bookcase 书架信息表 tb_booktype 图书类型表 ...

Hibernate中one-to-many的基本操作

数据库 drop database if exists stuInfo; create database if not exists stuInfo; use stuInfo; drop ta...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hibernate中的one-to-many
举报原因:
原因补充:

(最多只允许输入30个字)