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 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 图书类型表 ...

关于如何实现在整合struts2和hibernate4框架下对many-to-one级联数据的存储

在大多数的SSH框架教程上关于级联数据的存储往往是在Java Application下实现的。诸如User和Skill为many-to-one级联关系,User为many,Skill为one。 代码...

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

数据库 drop database if exists stuInfo; create database if not exists stuInfo; use stuInfo; drop ta...

Hibernate One to Many 迟延加载分析

One to Many   1: Oneto Many,默认是采用延迟加载的(lazy=true),因为很多情况下,当我们获取到One的一方后,Many的一方并非立即需要的,当我们经常只需要One...

Hibernate之one-to-many XML 篇二

接着上一篇文章中所简单叙述的one-to-many双向关系的配置以及使用,上章记录到了cascade、lazy。这章将继续完成inverse、feth以及not-found的简述: 回顾上章Us...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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