Hibernate 한 쌍 의 다 중 연결 저장, 삭제

7383 단어 J2EE
이 예 는 Hibernate 의 한 쌍 이상 의 연결 저장, 삭제 연습 에 사 용 됩 니 다.
관련 된 실체 류 는 Staff (다) 와 Company (1) 이다.
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 + "]";
	}

}

회사 류
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();
		}
	}
}

좋은 웹페이지 즐겨찾기