Hibernate一对多之级联保存、删除

本例子用来练习Hibernate一对多的级联保存、删除数据。

涉及到的实体类为Staff(多)和Company(一)

Staff类

package com.maty.entity;

/**
 * @author maty e-mail:[email protected]
 * @version 创建时间:2018年5月30日 上午11:27:11 类说明 员工类
 */
public class Staff
{
	private int sid;
	private String name;
	private String sex;
	private Company company;

	public int getSid()
	{
		return sid;
	}

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

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getSex()
	{
		return sex;
	}

	public void setSex(String sex)
	{
		this.sex = sex;
	}

	public Company getCompany()
	{
		return company;
	}

	public void setCompany(Company company)
	{
		this.company = company;
	}

	@Override
	public String toString()
	{
		return "Staff [sid=" + sid + ", name=" + name + ", sex=" + sex + ", company=" + company + "]";
	}

}

Company类

package com.maty.entity;

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

/**
 * @author maty e-mail:[email protected]
 * @version 创建时间:2018年5月30日 上午11:27:19 类说明
 */
public class Company
{
	private int cid;
	private String name;
	private String nature;
	private Set staffs = new HashSet();

	public int getCid()
	{
		return cid;
	}

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

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getNature()
	{
		return nature;
	}

	public void setNature(String nature)
	{
		this.nature = nature;
	}

	public Set getStaffs()
	{
		return staffs;
	}

	public void setStaffs(Set staffs)
	{
		this.staffs = staffs;
	}

}

Staff.hbm.xml




	
		
			
		
		
		
		
	

Company.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.test;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery;
import org.junit.Test;

import com.maty.entity.Company;
import com.maty.entity.Staff;
import com.maty.utils.HibernateUtil;

/**
 * @author maty e-mail:[email protected]
 * @version 创建时间:2018年5月30日 上午11:48:29 类说明 测试类
 */
public class MyTest
{
	// 测试是否能正确创建两个表
	@Test
	public void test()
	{
		HibernateUtil.getSessionFactory();
	}

	// 一对多的级联保存
	@Test
	public void add()
	{
		Transaction transaction = null;
		SessionFactory sessionFactory = null;
		Session session = null;
		try
		{
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			// 编写业务逻辑(级联添加数据)
			Staff staff1 = new Staff();
			staff1.setName("maty");
			staff1.setSex("man");
			Staff staff2 = new Staff();
			staff2.setName("jason");
			staff2.setSex("man");

			Company company = new Company();
			company.setName("npartner");
			company.setNature("台企");

			staff1.setCompany(company);
			staff2.setCompany(company);
			Set staffs = company.getStaffs();
			staffs.add(staff1);
			staffs.add(staff2);
			session.save(company);
			// ---------------------------------------------------------------------
			transaction.commit();
			System.out.println("程序正常执行成功");
		} catch (Exception e)
		{
			transaction.rollback();
		} finally
		{
			session.close();
			sessionFactory.close();
		}
	}

	// 一对多的级联delete
	@Test
	public void delete()
	{
		Transaction transaction = null;
		SessionFactory sessionFactory = null;
		Session session = null;
		try
		{
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			// 编写业务逻辑(级联添加数据)

			// 第一步:先要获取对象
			Company company = session.get(Company.class, 1);
			session.delete(company);

			// ---------------------------------------------------------------------
			transaction.commit();
			System.out.println("程序正常执行成功");
		} catch (Exception e)
		{
			transaction.rollback();
		} finally
		{
			session.close();
			sessionFactory.close();
		}
	}

	// 使用sqlquery来查询
	@Test
	public void Query()
	{
		Transaction transaction = null;
		SessionFactory sessionFactory = null;
		Session session = null;
		try
		{
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			transaction = session.beginTransaction();
			// 编写业务逻辑(级联添加数据)

			String sql = "select * from t_staff where cid = ?";
			NativeQuery sqlQuery = session.createSQLQuery(sql);
			sqlQuery.addEntity(Staff.class);
			sqlQuery.setParameter(1, "2");
			List list = sqlQuery.list();
			for (Staff staff : list)
			{
				System.out.println(staff);
			}

			// ---------------------------------------------------------------------
			transaction.commit();
			System.out.println("程序正常执行成功");
		} catch (Exception e)
		{
			transaction.rollback();
		} finally
		{
			session.close();
			sessionFactory.close();
		}
	}
}


你可能感兴趣的:(J2EE)