J2EE之路(七)hibernate(03)关联映射

关联映射

  • 多对一(Employee - Department)
  • 一对多(Department - Employee)
  • 一对一(person - idcard)
  • 多对多(teacher - student)
  • 组合映射(User - Name)
  • 集合映射(set,list,map,sag)
  • inverse和cascade(Employ - department)

多对一映射(Employee = Department)

映射文件<many-to-one name="depart" column="depart_id"/>

可以看出,我们可以只关心对象模型,关系模型由hibernte来维护。

对应一下Department类

package domain;

public class Employee {
	private int id;
	private String name;
	private Department 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;
	}
}

 可以看出Employee类持有department类的引用,在配置文件中只要把属性放进名字即可。

看一下自动生生成的Employee表的建表语句,果然生成了外键。

PS:只是标签从property编程了many-to-one其他的貌似都没变

测试一下:

package test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import Util.HibernateUtils;
import domain.Department;
import domain.Employee;
import domain.User;

public final class TestMany2One {
	public static void main(String[] args) {
		add();
		query(1);
	}

	private static void add() {
		Session session = null;
		Transaction transaction = null;
		try{
			session = HibernateUtils.getSession();
			transaction = session.beginTransaction();
			Department depart = new Department();
			depart.setName("depart01");
			Employee employee = new Employee();
			employee.setName("employee01");
			employee.setDepart(depart);//对象模型:建立两个对象的关联
			session.save(depart);
			session.save(employee);
			transaction.commit();
		}catch(Exception e){
			e.printStackTrace();
			transaction.rollback();
		}finally{
			if(session != null)
				session.close();
		}
	}
	
	private static Employee query(int id) {
		Session session = null;
		try{
			session = HibernateUtils.getSession();
			Employee employee = (Employee) session.get(Employee.class,id);
			System.out.println(employee.getDepart().getName());
			return employee;
		}finally{
			if(session != null)
				session.close();
		}
	}
}

 一对多(Department - Employee)

 在Department类中增加属性Set<Employee> emps 表示一对多

在Deppartment的配置文件中增加

<set name="emps">

 <key column="depart_id"/>

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

</set>

一对一(person - idcard)

基于主键的one-to-one(person的映射文件)

<id name="id">

 <generator class="foreign">

<param name="property">idcard<param>

</generator>

<id>

<one-to-one name="idCard" constrained="true">

多对多(teacher - student)

在操作和性能方面都不太理想,所以多对多映射使用较少,实际使用中最好转换成一对多的模型,Hibernate会为我们创建中间关联表,转换成两个一对多。

<set name="teacher" table="teacher_student">

<key column="teacher_id">

<many-to-many class="student" column="student">

</>

组合映射(User - Name)

感觉就是这个对象不是很大,不用使用一张表来表示他比如姓和名直接放进User表就好

标签如下:

<compoment name="name">

<property name="firstName" column="first_name">

<property name="lastName" column="last_name">

</compoment>

最后零散笔记吧

级联操作:<set name="emps" cascade="save-update">

一对多,多对一是相互的,一般一的这一方面放弃对关系的维护:<set name="emps" inverse="true">

以上只能是set,list不能指定inverse,所以一对多最好不用List,而用set

相关推荐