Hibernate关联映射 --- 一对多实例分析(双向关联)

Hibernate关联映射 --- 一对多实例分析(双向关联)

一 概念

最典型的例子是Department和Employee的关系,双向的关联

二 代码分析

(1)Department表

package com.hbsi.domain;

import java.util.Set;

//部门类

public class Department {

    //实现一对多

    private int id;

    private String nameString;

    private Set<Employee> emps; //集合类型,因为有多个员工

    public Department() {

       super();

       // TODO Auto-generated constructor stub

    }

    public Department(int id, String nameString, Set<Employee> emps) {

       super();

       this.id = id;

       this.nameString = nameString;

       this.emps = emps;

    }

    public int getId() {

        return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getNameString() {

       return nameString;

    }

    public void setNameString(String nameString) {

       this.nameString = nameString;

    }

    public Set<Employee> getEmps() {

       return emps;

    }

    public void setEmps(Set<Employee> emps) {

       this.emps = emps;

    }

    @Override

    public String toString() {

       return "Department [id=" + id + ", nameString=" + nameString

              + ", emps=" + emps + "]";

    }

}

(2)Employee类

package com.hbsi.domain;

//员工类      一般主鍵是建在多的一方

public class Employee {

    private int id;

    private String name;

//通过id查询员工,通过员工 查找部门的话,只能找到部门的id,得不到部门的其他信息

    // 得到的是一个 对象,可以得到员工对应的部门的详细信息

    private Department depart;

    public Employee() {

       super();

       // TODO Auto-generated constructor stub

    }

    public Employee(int id, String name, Department depart) {

       super();

       this.id = id;

       this.name = name;

       this.depart = depart;

    }

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public Department getDepart() {

       return depart;

    }

    public void setDepart(Department depart) {

       this.depart = depart;

    }

    public String toString() {

       return "Employee [id=" + id + ", name=" + name + ", depart=" + depart

              + "]";

    }  

}

(3)配置文件

<!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:///demo</property>

       <property name="hibernate.connection.username">root</property>

       <property name="hibernate.connection.password">1234</property>

       <!-- 方言 针对哪个数据库Mysql -->

       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

       <!-- 在程序运动的时候,增加自动创建表的属性,在程序终止 的时候销毁,但是在表格再次使用时,会重新建 -->

       <property name="hibernate.hbm2ddl.auto">update</property>

       <!-- 执行的sql语句显示出来 -->

       <property name="hibernate.show_sql">true</property>

       <!-- 指定映射文件的位置 -->

       <mapping resource="com/hbsi/domain/Department.hbm.xml" />

       <mapping resource="com/hbsi/domain/Employee.hbm.xml" />

    </session-factory>

</hibernate-configuration>

(4)Department的映射文件

    <?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.hbsi.domain">

    <!-- 缺省table 表明和类名是一样的 -->

    <class name="Department" table="department">

       <id name="id" column="id">

           <generator class="native" />

       </id>

       <property name="name" column="name" />

       <!-- 集合属性的体现  一对多 -->

       <set name="emps">

           <!--  根据外键的值查询,而不是查询所有的记录-->

           <key column="depart_id"/>

           <!-- 告诉Hibernate emps是集合属性,是一对多的关联 -->

           <one-to-many class="Employee" />  

       </set>

    </class>

</hibernate-mapping>

(5)Employee的映射文件

<?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.hbsi.domain">

    <class name="Employee" table="employee">

       <id name="id" column="id">

           <generator class="native" />

       </id>

       <property name="name" column="name" />

       <!-- 外键的映射 (多对一)  not-null="true"是使外键不为空 -->

       <many-to-one name="depart" column="depart_id" not-null="true" foreign-key="指定外键的名称"/>

    </class>

</hibernate-mapping>

 

(6)测试插入和查询

package com.hbsi.test;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.hbsi.domain.Department;

import com.hbsi.domain.Employee;

import com.hbsi.hibernate.utils.HibernateUtil;

public class One2Many {

    public static void main(String[] args) {

       //add();

       queryDepartment(5);

    }

    static Department add() {

       Session session = null;

       Transaction transaction = null;

       try {

           session = HibernateUtil.getSession();

           transaction = session.beginTransaction();

           // 添加部门

           Department department = new Department();

           department.setName("老王公司 one");

 

           Employee employee1 = new Employee();

           employee1.setName("老王");

           employee1.setDepart(department);// 员工对象和部门对象建立关联

 

           Employee employee2 = new Employee();

           employee2.setName("老王王");

           employee2.setDepart(department);

 

           session.save(department);

           session.save(employee1);

           session.save(employee2);

 

           transaction.commit();

           return department;

       } finally {

           if (session != null) {

              session.close();

           }

       }

    }

    // 查询部门的所有员工

    static Department queryDepartment(int depId) {

       Session session = null;

       try {

           session = HibernateUtil.getSession();

           Department department = (Department) session.get(Department.class,depId);

           System.out.println("部门名字为:"+department.getName()+"--"+department.getEmps().size()+"个员工");

           return department;

 

查询姓名并遍历输出

Set<Employee> list = department.getEmps();

           for(Employee entities:list){

              System.out.println(entities.getName());

           }

       } finally {

           if (session != null) {

              session.close();

           }

       }

    }

}

注:

(1)在一的一方映射文件中的最重要的是

    <set name="emps">

           <!--  根据外键的值查询,而不是查询所有的记录-->

           <key column="depart_id"/>

           <!-- 告诉Hibernate emps是集合属性,是一对多的关联 -->

           <one-to-many class="Employee" />  

    </set>

(2)在Department中设置的是set集合类型的

   

   

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值