Hibernate 多对一小案例

来源:互联网 发布:中国名校网络教育联盟 编辑:程序博客网 时间:2024/06/05 09:11

1.创建实体类Dept和Emp以及小配置

package cn.happy.manytoone;/** * Created by guo on 2017/10/6. */public class Dept {    private int deptno;    private String deptname;    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.manytoone">    <class name="Dept" schema="guohua">        <id name="deptno">            <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID -->            <generator class="native"></generator>        </id>        <property name="deptname"/>    </class></hibernate-mapping>
--------------------------------------------------------------------------------------
package cn.happy.manytoone;/** * 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.manytoone">    <class name="Emp" schema="guohua">        <id name="empno">            <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID -->            <generator class="native"></generator>        </id>        <property name="ename"/>        <many-to-one name="dept" column="deptno" class="Dept"></many-to-one>    </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.manytoone.Emp;import cn.happy.util.sessionUtil;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;/** * Created by guo on 2017/10/6. */public class manytoone {    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();        Emp emp = session.get(Emp.class, 1);        System.out.println(emp.getDept().getDeptname());        tx.commit();        session.close();    }}

原创粉丝点击