Hibernate 一对多案例

1.编写实体类Dept和Emp以及小配置

package cn.happy.onetomanydouble;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by guo on 2017/10/6.
 */
public class Dept {
    private int deptno;
    private String deptname;
    private Set<Emp> emps=new HashSet<Emp>();

    public Set<Emp> getEmps() {
        return emps;
    }

    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }
}
----------------------------------------------------------------------------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernaate.net/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.onetomanydouble">
    <class name="Dept" schema="guohua">
        <id name="deptno">
            <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID -->
            <generator class="native"></generator>
        </id>
        <property name="deptname"/>
        <set name="emps" order-by=" empno desc " cascade="save-update" inverse="true">
            <key column="deptno"></key>
            <one-to-many class="Emp"></one-to-many>
        </set>

    </class>
</hibernate-mapping>
-----------------------------------------------------------------------------------------------------------------------------------------------------

package cn.happy.onetomanydouble;

/**
 * Created by guo on 2017/10/6.
 */
public class Emp
{
    private int empno;
    private String ename;
    private Dept dept=new Dept();

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }
}
------------------------------------------------------------------------------------------------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernaate.net/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.onetomanydouble">
    <class name="Emp" schema="guohua">
        <id name="empno">
            <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID -->
            <generator class="native"></generator>
        </id>
        <property name="ename"/>

    </class>
</hibernate-mapping>
----------------------------------------------------------------------------------------------------------

2.编写大配置文件

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 数据库链接-->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">guohua</property>
        <property name="connection.password">123456</property>
        <!-- 辅助参数-->
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="format_sql">true</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
       <!-- <property name="current_session_context_class">thread</property>-->
        <mapping resource="cn/happy/manytoone/Dept.hbm.xml"/>
        <mapping resource="cn/happy/manytoone/Emp.hbm.xml"/>
        <mapping resource="cn/happy/onetomanydouble/Dept.hbm.xml"/>
        <mapping resource="cn/happy/onetomanydouble/Emp.hbm.xml"/>
        <mapping resource="cn/happy/manytomany/Employee.hbm.xml"/>
        <mapping resource="cn/happy/manytomany/Project.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
--------------------------------------------------------------------------------------------------------------------------------------------

3.编写测试类

package cn.happy.test;


import cn.happy.onetomanydouble.Dept;
import cn.happy.onetomanydouble.Emp;
import cn.happy.util.sessionUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

/**
 * Created by guo on 2017/10/6.
 */
public class onttomanydouble {
    public static void main(String[] args) {
        Configuration cfg = new Configuration().configure();
        SessionFactory factory = cfg.buildSessionFactory();
        //Session session = factory.openSession();
        Session session = sessionUtil.getSession();
        Transaction tx = session.beginTransaction();

        List<Dept> list = session.createQuery("from Dept").list();
        for (Dept dept:list) {
            System.out.println(dept.getDeptname());
            System.out.println("================");
            for (Emp emp:dept.getEmps()) {
                System.out.println(emp.getEname());
            }
        }

        tx.commit();
        session.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值