深入浅出Hibernate(二)多对一关系映射

2014-11-23 20:19:47 · 作者: · 浏览: 9

学习Hibernate是为了更方便的操作数据库,在数据库中的关系模型中存在多对一的关系,比如下图所示的员工和部门之间的关系,那么这种关系在Hibernate中如何映射呢?让我用一个小Demo来详细讲解。

\

建立映射分为以下几步:

1.设计domain对象Department、Employee,代码如下:

package cn.itcast.hibernate.domain;

public class Department {
	private int id;
	private String 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;
	}
}
package cn.itcast.hibernate.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;
	}
}

2.导入hibernate.jar和其依赖的包,jar包的 下载地址在上面博客中已经提供,这里不再赘述。

3.编写Department.hbm.xml和Employee.hbm.xml映射文件,代码实现如下:

    
  

    
    
    
     
      
     
     
     

    

    
  

     
    
    
     
      
     
     
     
     

    
Employee.hbm.xml中的“many-to-one”部分是重点,通过它建立Department对象和Employee的关联关系。

4.编写hibernate.cfg.xml配置文件,代码实现如下:



  
	
    
    
     true
     
    
     org.hibernate.dialect.MySQLDialect
     
    
     com.mysql.jdbc.Driver
     
    
     jdbc:mysql://localhost:3306/test
     
    
     root
     
     
    
     update
     
     
     
     
   

   
5.编写HibernateUtils类,主要用来完成Hibernate初始化和提供一个获得Session的方法,代码实现如下:
package com.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public final class HibernateUtil {
	private static SessionFactory sessionFactory;
	
	private HibernateUtil(){
	}
	
	static {
		//解析配置文件,完成Hibernate初始化
		Configuration cfg=new Configuration();
		cfg.configure();
		//所有的信息在sessionFactory中都可以找到
		sessionFactory=cfg.buildSessionFactory();
	}
	
	public static SessionFactory getSesssionFactory(){
		return sessionFactory;
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
	}
}
6.编写Main方法,通过操作Department类和Employee类,向 数据库中写入数据,代码实现如下:
package com.entity;

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

import cn.itcast.hibernate.domain.Department;
import cn.itcast.hibernate.domain.Employee;

public class Many2One {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		add();
	}

	static Department add() {
		Session s = null;
		Transaction tx = null;
		try {
			Department depart = new Department();
			depart.setName("depart name");

			Employee emp = new Employee();
			emp.setDepart(depart);
			emp.setName("emp name");

			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(depart);
			s.save(emp);
			tx.commit();
			return depart;
		} finally {
			if (s != null)
				s.close();
		}
	}
}
让我们查看一下数据表的变化,效果图如下所示:


通过上述方法,我们就可以轻松完成多对一关系从关系模型到对象模型的映射,完美的体现了ORM的思想。

希望我的讲解能帮助大家学习Hibernate。