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 emps;//集合类型,因为有多个员工

public Department() {

super();

// TODO Auto-generated constructor stub

}

public Department(int id, String nameString, Set 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 getEmps() {

return emps;

}

public void setEmps(Set 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)配置文件

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

"hibernate.connection.driver_class">com.mysql.jdbc.Driver

"hibernate.connection.url">jdbc:mysql:///demo

"hibernate.connection.username">root

"hibernate.connection.password">1234

"hibernate.dialect">org.hibernate.dialect.MySQLDialect

"hibernate.hbm2ddl.auto">update

"hibernate.show_sql">true

"com/hbsi/domain/Department.hbm.xml" />

"com/hbsi/domain/Employee.hbm.xml" />

(4)Department的映射文件

"1.0"?>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

"com.hbsi.domain">

"Department" table="department">

"id" column="id">

"native" />

"name" column="name" />

"emps">

"depart_id"/>

"Employee" />

(5)Employee的映射文件

"1.0"?>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

"com.hbsi.domain">

"Employee" table="employee">

"id" column="id">

"native" />

"name" column="name" />

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

(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 list = department.getEmps();

for(Employee entities:list){

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

}

} finally {

if (session != null) {

session.close();

}

}

}

}

注:

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

"emps">

"depart_id"/>

"Employee" />

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

你可能感兴趣的:(Hibernate关联映射 --- 一对多实例分析(双向关联))