Hibernate之多对多级联保存以及删除

对应的两个实体类为Student和Course

Student类

package com.maty.entity;

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

/**
 * @author maty e-mail:[email protected]
 * @version 创建时间:2018年5月31日 下午3:25:32 类说明 学生类
 */
public class Student
{
	private int sid;
	private String s_name;
	private String s_sex;
	private Set setCourse = new HashSet();

	public Set getSetCourse()
	{
		return setCourse;
	}

	public void setSetCourse(Set setCourse)
	{
		this.setCourse = setCourse;
	}

	public int getSid()
	{
		return sid;
	}

	public void setSid(int sid)
	{
		this.sid = sid;
	}

	public String getS_name()
	{
		return s_name;
	}

	public void setS_name(String s_name)
	{
		this.s_name = s_name;
	}

	public String getS_sex()
	{
		return s_sex;
	}

	public void setS_sex(String s_sex)
	{
		this.s_sex = s_sex;
	}

}

Course类

package com.maty.entity;

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

/**
 * @author maty e-mail:[email protected]
 * @version 创建时间:2018年5月31日 下午3:27:03 类说明 课程类
 */
public class Course
{
	private int cid;
	private String c_name;
	private String c_desc;
	private Set setStudent = new HashSet();

	public Set getSetStudent()
	{
		return setStudent;
	}

	public void setSetStudent(Set setStudent)
	{
		this.setStudent = setStudent;
	}

	public int getCid()
	{
		return cid;
	}

	public void setCid(int cid)
	{
		this.cid = cid;
	}

	public String getC_name()
	{
		return c_name;
	}

	public void setC_name(String c_name)
	{
		this.c_name = c_name;
	}

	public String getC_desc()
	{
		return c_desc;
	}

	public void setC_desc(String c_desc)
	{
		this.c_desc = c_desc;
	}

}

Student.hbm.xml




	
		
			
		
		
		
		
		
			
			
		
	

Course.hbm.xml




	
		
			
		
		
		
		
			
			
		
	

hibernate.cfg.xml





	
		
		
		org.hibernate.dialect.MySQL5Dialect
		
		com.mysql.jdbc.Driver
		
		jdbc:mysql://localhost/tx
		
		root
		
		wangxiaowei

		
		
		true
		
		true
		
		update

		
		
		
	

工具类

package com.maty.utils;

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

/** 
* @author maty  e-mail:[email protected]
* @version 创建时间:2018年5月30日 上午11:43:10 
* 类说明 工具类
*/
public class HibernateUtil
{
	private final static SessionFactory sessionFactory;
	static
	{
		Configuration cfg = new Configuration();
		cfg.configure();
		sessionFactory =  cfg.buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}
}
 

测试类

package com.maty.test;

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

import com.maty.entity.Course;
import com.maty.entity.Student;
import com.maty.utils.HibernateUtil;

/**
 * @author maty e-mail:[email protected]
 * @version 创建时间:2018年5月31日 下午3:40:40 类说明 测试类
 */
public class MyTest
{
	// 测试级联保存
	@Test
	public void CreateTable()
	{
		HibernateUtil.getSessionFactory();
		System.out.println("表格创建成功");
	}

	// 测试级联保存数据
	@Test
	public void save()
	{
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction transaction = null;
		try
		{
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
//----------------------------------------------------------------------
			// 编写业务逻辑部分
			Student student = new Student();
			student.setS_name("tye");
			student.setS_sex("男");
			Course course1 = new Course();
			course1.setC_name("c++");
			course1.setC_desc("一种面向过程的开发语言");
			Course course2 = new Course();
			course2.setC_name("PHP");
			course2.setC_desc("世界上最好的语言");
			
			
			
			
			student.getSetCourse().add(course1);
			student.getSetCourse().add(course2);
			session.save(student);
//----------------------------------------------------------------------
			transaction.commit();
			System.out.println("数据保存成功");
		} catch (Exception e)
		{
			transaction.rollback();
		} finally
		{
			if(session != null)
				session.close();
			if(sessionFactory != null)
				sessionFactory.close();
		}
	}

	// 删除相关数据
	@Test
	public void del()
	{
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction transaction = null;
		try
		{
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			// ----------------------------------------------------------------------
			// 编写业务逻辑部分
			//获取Student
			Student student = session.get(Student.class, 1);
			Course course = session.get(Course.class, 2);
			student.getSetCourse().remove(course);
			
			// ----------------------------------------------------------------------
			transaction.commit();
			System.out.println("数据删除成功");
		} catch (Exception e)
		{
			transaction.rollback();
		} finally
		{
			if (session != null)
				session.close();
			if (sessionFactory != null)
				sessionFactory.close();
		}
	}
}

你可能感兴趣的:(J2EE)