Hibernate关联映射---多对一实例分析(单项关联)

本文介绍了一种多对一的关联映射实现方式,通过具体案例解析了如何利用Hibernate框架进行部门与员工间的关联映射,包括创建实体类、配置映射文件及测试保存与查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关联映射---多对一实例分析

一 关联映射类型

1.多对一(Employee - Department)

多对一关联是一个部门可以有多个员工

所用到的映射文件为:

<many-to-one name="depart" column="depart_id"/>

二 代码分析

1.首先创建两个类,Department和Employee,并把他们关联起来

(1)Department类

package com.hbsi.domain;

//部门类

public class Department {

private int id;

private String name;

public Department() {

super();

// TODO Auto-generated constructor stub

}

public Department(int id, String name) {

super();

this.id = id;

this.name = name;

}

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 String toString() {

return "Department [id=" + id + ", name=" + name + "]";

}

}

(2)Employee类

package com.hbsi.domain;

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

public class Employee {

private int id;

private String name;

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

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

+ "]";

}

}

注:之所以在员工表中设主键是为了减少资源消耗,而如果设置

private int depart_id;

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

设置的Deparment的类对象作为外键,可以得到部门的所有信息

(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" />

</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" />

<!-- 外键的映射 (多对一) -->

<many-to-one name="depart" column="depart_id"not-null="true"/>

</class>

</hibernate-mapping>

注:一般情况下,主键是不为空的,所以应该在<many-to-one>标签中加一条语句 not-null="true"

(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 Many2One {

public static void main(String[] args) {

add();

}

//保存操作

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);// 对象模型:对象建立关联关系

//保存操作

session.save(department);

session.save(employee1);

transaction.commit();

return department;

} finally {

if (session != null) {

session.close();

}

}

}

}

注:

需要注意的是下面两句话顺序不同,执行的结果不同,

(1)如果是先保存employee,即操作为:

session.save(employee1);

session.save(department);

结果为

Hibernate: insert into employee (name, depart_id) values (?, ?)

Hibernate:insert into department (name) values (?)

Hibernate: update employee set name=?, depart_id=? where id=?

(2)如果一的一方先执行,即操作的是

session.save(department);

session.save(employee1);

执行的语句为:

Hibernate: insert into department (name) values (?)

Hibernate: insert into employee (name, depart_id) values (?, ?)

//通过查询员工表的id获取到部门表的name

static Employee query(int empId) {

Session session = null;

try {

session = HibernateUtil.getSession();

Employee employee = (Employee) session.get(Employee.class, empId);

System.out.println(employee.getName()+"=="+employee.getDepart().getName());

return employee;

} finally {

if (session != null) {

session.close();

}

}

}

注:

(1)如果把输出的语句

System.out.println(employee.getName()+"=="+employee.getDepart().getName());

放到主方法中会抛异常:

Hibernate: select employee0_.id as id2_0_, employee0_.name as name2_0_, employee0_.depart_id as depart3_2_0_ from employee employee0_ where employee0_.id=?

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session

at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)

at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)

at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)

at com.hbsi.domain.Department_$$_javassist_1.getName(Department_$$_javassist_1.java)

at com.hbsi.test.Many2One.main(Many2One.java:16)

原因是懒加载超时异常,因为可以看出只输出一条语句,没有查找出DEepartment,因为它就相当于懒加载

(2)如果非要把输出语句放到 方法中,比避免抛异常,可以在执行的时候写一条语句用来初始化Department:Hibernate.initialize(employee.getDepart());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值