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

2021-07-05 21:05

阅读:697

标签: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.Driverproperty>
                property name="hibernate.connection.url">jdbc:mysql:///webprojectproperty>
                property name="hibernate.connection.username">rootproperty>            
                property name="hibernate.connection.password">123456property>            
                property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>
                property name="hibernate.show_sql">trueproperty>
                property name="hibernate.hbm2ddl.auto">createproperty>
                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>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 setEmployees = new HashSet();

    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 getSetEmployees() {
        return setEmployees;
    }

    public void setSetEmployees(Set 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


评论


亲,登录后才可以留言!