码迷,mamicode.com
首页 > Web开发 > 详细

hibernate之初学一对多和多对一配置及使用

时间:2017-07-02 14:22:13      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:uil   java   .hbm.xml   import   entity   使用   oct   connect   throws   

按查询及存取速率来说的一对多用的相对多对一少一些,这里只写主要配置文件的代码

首先是hibernate的配置文件

<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.url">jdbc:mysql:///webproject</property>
                <property name="hibernate.connection.username">root</property>            
                <property name="hibernate.connection.password">123456</property>            
                <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
                <property name="hibernate.show_sql">true</property>
                <property name="hibernate.hbm2ddl.auto">create</property>
                <mapping resource="com/shaoxin/entity/Dept.hbm.xml"/>
                <mapping resource="com/shaoxin/entity/Employee.hbm.xml"/>
            </session-factory>
        </hibernate-configuration>

一对多的配置文件代码,很明显使用到了set集合

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.shaoxin.entity">
        <class name="Dept" table="dept">
            <id name="deptid" column="deptid">
                <generator class="native"></generator>
            </id>
            <property name="deptName" column="deptname"></property><!--
            一对多设置外键方式,class代表外键类型
            --><set name="setEmployees" table="employee">
                <key column="dept_id"></key>
                <one-to-many class="Employee"/>
            </set>
        </class>
    </hibernate-mapping>

对应的实体类:

package com.shaoxin.entity;

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

public class Dept {
    private int deptid;
    private String deptName;
    private Set<Employee> setEmployees = new HashSet<Employee>();

    public int getDeptid() {
        return deptid;
    }

    public void setDeptid(int deptid) {
        this.deptid = deptid;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Set<Employee> getSetEmployees() {
        return setEmployees;
    }

    public void setSetEmployees(Set<Employee> setEmployees) {
        this.setEmployees = setEmployees;
    }

}

多对一的配置文件代码,很明显是一个对象类型

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.shaoxin.entity">
        <class name="Employee"  table="employee">
            <id name="employeeid" column="employeeid">
                <generator class="native"></generator>
            </id>
            <property name="employeeName" column="employeename"/><!--
            多对一创建外键方式:属性分别为:外键对应的表,外键字段,外键类型
            --><many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>
        </class>
    </hibernate-mapping>

对应的实体类

package com.shaoxin.entity;

public class Employee {
    private int employeeid;
    private String employeeName;
    private Dept dept;

    public int getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(int employeeid) {
        this.employeeid = employeeid;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public Dept getDept() {
        return dept;
    }

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

}

测试这两个存储方式

package com.shaoxin.entity;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;

public class TestMany2Many {
    static SessionFactory sf;
    static {
        sf = new Configuration().configure().buildSessionFactory();
    }

    @Test
    public void testOne2Many() throws Exception {
        Session openSession = sf.openSession();
        Transaction beginTransaction = openSession.beginTransaction();
        Employee empzs = new Employee();
        Employee empls = new Employee();
        empls.setEmployeeName("李四");
        empzs.setEmployeeName("张三");
        Dept dept = new Dept();
        dept.getSetEmployees().add(empls);
        dept.getSetEmployees().add(empzs);
        dept.setDeptName("应用开发");
        openSession.save(empls);
        openSession.save(empzs);
        openSession.save(dept);
        beginTransaction.commit();
        openSession.close();
    }

    @Test
    public void testMany2One() throws Exception {
        Session openSession = sf.openSession();
        Transaction beginTransaction = openSession.beginTransaction();
        Employee empzs = new Employee();
        Employee empls = new Employee();
        Dept dept = new Dept();
        dept.setDeptName("应用开发");
        empls.setEmployeeName("李四");
        empzs.setEmployeeName("张三");
        empls.setDept(dept);
        empzs.setDept(dept);
        openSession.save(dept);
        openSession.save(empls);
        openSession.save(empzs);
        beginTransaction.commit();
        openSession.close();
    }
}

 

hibernate之初学一对多和多对一配置及使用

标签:uil   java   .hbm.xml   import   entity   使用   oct   connect   throws   

原文地址:http://www.cnblogs.com/ShaoXin/p/7105837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!