Hibernateday03一对多双向操作

1.建表

create  table g_dept(
	t_id integer primary key,
	t_dname varchar2(20),
	t_dno varchar2(20)
)

create table g_employee(
	t_id integer primary key,
	t_name varchar2(25),
	t_birthday date,
	t_salary number(7,2),
	t_email varchar2(30),
	d_id integer references g_dept(t_id)
)

 2.在com.jsu.hb.pojo包中提供两个实体类Employee.java和Dept.java中

 在Employee.java中

package com.jsu.hb.pojo;

import java.util.Date;

public class Employee {
	private Integer id;
	private String name;
	private Date birthday;
	private String email;
	private double salary;
	private Dept dept;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	
}

 在Dept.java中

package com.jsu.hb.pojo;

import java.util.HashSet;
import java.util.Set;

public class Dept {
	private Integer id;
	private String dname;
	private String dno;
	private Set<Employee> emps = new HashSet<Employee>();
	
	//提供工具类,明确对象在内存中的关系
	public void addEmp(Employee e){
		this.emps.add(e);//设定Dept和Employee的关系
		e.setDept(this);//设定Employee和Dept的关系
	}
	// 解除单个对象的关系
	public void removeEmp(Employee e) {
		this.emps.remove(e);
		e.setDept(null);
	}
	//解除所有员工的关系
	public void removeAll(){
		for(Employee e:emps){
			e.setDept(null);
			}
		this.emps.clear();
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getDno() {
		return dno;
	}
	public void setDno(String dno) {
		this.dno = dno;
	}
	public Set<Employee> getEmps() {
		return emps;
	}
	public void setEmps(Set<Employee> emps) {
		this.emps = emps;
	}
	
}

 3.提供映射文件o2m.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping  package="com.jsu.hb.pojo">
	<class name="Dept" table="g_dept">
		<id name="id" column="t_id">
			<generator class="increment"></generator>
		</id>
		<property name="dname" column="t_dname"></property>
		<property name="dno" column="t_dno"></property>
		<!-- 关系属性 
		双向关系时 设置inverse="true":使<key 标签失效,阻止重复更新的发生,
		目的减少与数据库的通讯保证了数据库的安全
		-->
		<set name="emps" cascade="save-update" inverse="true">
			<key column="d_id"></key><!-- 外键信息 -->
			<one-to-many class="Employee"></one-to-many>
		</set>
	</class>
	<class name="Employee" table="g_employee">
		<id name="id" column="t_id">
			<generator class="increment"></generator>
		</id>
		<property name="name" column="t_name"></property>
		<property name="birthday" column="t_birthday" type="java.util.Date"></property>
		<property name="email" column="t_email"></property>
		<property name="salary" column="t_salary"></property>
		 <!-- 双向关系是添加关系属性 -->
	   <many-to-one name="dept" class="Dept" cascade="save-update" column="d_id"></many-to-one>
	</class>
</hibernate-mapping>

 4.在hibernate.cfg.xml文件中对映射文件进行注册

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory> 
		<!-- show_sql:是否显示hibernate执行的SQL语句,默认为false -->
		<property name="show_sql">true</property>
		<!-- show_sql:是否显示hibernate执行格式化输出的SQL语句,默认为false -->
		<property name="format_sql">true</property>
		<!-- 配置与数据库连接的参数 -->
		<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
		<property name="connection.username">scott</property>
		<property name="connection.password">tiger</property>
		<!-- 2.自身属性相关的配置
			dialect:方言
			hibernate根据dialect的配置进行特定数据性能方面的调优
		 -->
		<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
		<mapping resource="com/jsu/hb/pojo/o2m.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>

 5.提供HibernateUtil.java工具类获得session

package com.jsu.hb.util;

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

public class HibernateUtil {
	private static SessionFactory sf;
	private static ThreadLocal<Session> tl= new ThreadLocal<Session>();
	static{
		try{
				Configuration cfg = new Configuration();
				cfg.configure();
				sf=cfg.buildSessionFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static Session openSession(){
		return sf.openSession();
	}
	public static Session getCurrentSession(){
		Session session = tl.get();//先从储存的线程中查找
		if(session==null){
			session=openSession();
			tl.set(session);
			return session;
		}
		return session;
	}
}

 6.提供测试类TestO2M.java

package com.jsu.hb.test;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.jsu.hb.pojo.Dept;
import com.jsu.hb.pojo.Employee;
import com.jsu.hb.util.HibernateUtil;

public class TestO2M {
	@Test
	public void save(){
		Dept d = new Dept();
		d.setDname("技术部");
		d.setDno("1001");
		
		Employee e1 = new Employee();
		e1.setName("zhangsan");
		e1.setBirthday(new Date());
		e1.setEmail("[email protected]");
		e1.setSalary(1234.0);
		d.addEmp(e1);// 设定双向关系
		
		Employee e2 = new Employee();
		e2.setName("tom");
		e2.setBirthday(new Date());
		e2.setEmail("[email protected]");
		e2.setSalary(2345.5);
		d.addEmp(e2);// 设定双向关系
		
		Employee e3 = new Employee();
		e3.setName("leon");
		e3.setBirthday(new Date());
		e3.setEmail("[email protected]");
		e3.setSalary(2351.6);
		d.addEmp(e3);// 设定双向关系
		
		
		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = session.getTransaction();
		tx.begin();
		session.save(d);
		tx.commit();
	}
	//删除
	@Test
	public void del(){
		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = session.getTransaction();
		tx.begin();
		//Employee e = (Employee)session.get(Employee.class, 4);
		//session.delete(e);//删除成功,设置级联 cascade=save-update 无须解除关系可以直接删除
		Dept d = (Dept) session.get(Dept.class, 3);
	 	d.removeAll();
	 	/* 在Dept的配置中可以获取外键信息 <key column="xxx"/>
	 	 * 但是当我们设置 inverse="true"时,使key标签失效,不能获取外键信息
	 	 * 所有要现在内存中解除与employee的关系在删除
	 	 * */
	 	session.delete(d);
		tx.commit();
	}
}
 

你可能感兴趣的:(Hibernate)