05_hibenate 지연 로드

3529 단어 Hibernate

import org.hibernate.Session;
import org.junit.Test;
import com.wang.itat.model.Classroom;
import com.wang.itat.model.Student;
import com.wang.itat.util.HibernateUtil;

public class TestManyToOne {
	@Test
	public void testAdd01() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			//   1
			Classroom c = new Classroom();
			c.setGrade(2012);
			c.setName("       ");
			session.save(c);
			Student stu1 = new Student();
			stu1.setName("   ");
			stu1.setNo("001");
			stu1.setClassroom(c);
			session.save(stu1);
			Student stu2 = new Student();
			stu2.setName("   ");
			stu2.setNo("002");
			stu2.setClassroom(c);
			session.save(stu2);
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			if(session!=null) session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
	}
	
	@Test
	public void testAdd02() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			//    
			Student stu1 = new Student();
			stu1.setName("  ");
			stu1.setNo("003");
			session.save(stu1);
			Student stu2 = new Student();
			stu2.setName("  ");
			stu2.setNo("004");
			session.save(stu2);
			Classroom c = new Classroom();
			c.setGrade(2012);
			c.setName("       ");
			session.save(c);
			//       update
			stu1.setClassroom(c);
			stu2.setClassroom(c);
			//    :          ,         
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			if(session!=null) session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
	}
	
	@Test
	public void testAdd03() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Classroom c = new Classroom();
			c.setGrade(2012);
			c.setName("       ");
			//  classroom    ,     student       ,     
			Student stu1 = new Student();
			stu1.setName("  ");
			stu1.setNo("005");
			session.save(stu1);
			Student stu2 = new Student();
			stu2.setName("  ");
			stu2.setNo("006");
			session.save(stu2);
			stu1.setClassroom(c);
			stu2.setClassroom(c);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			if(session!=null) session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
	}
	
	@Test
	public void testDelete01() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Student stu = (Student)session.load(Student.class, 7);
			session.delete(stu);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			if(session!=null) session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
	}
	
	@Test
	public void testLoad() {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Student stu = (Student)session.load(Student.class, 1);
			//         sql
			System.out.println(stu.getName());
			//  student     Classroom       ,     sql    
			System.out.println(stu.getClassroom().getName());
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			if(session!=null) session.getTransaction().rollback();
		} finally {
			HibernateUtil.close(session);
		}
	}
}

좋은 웹페이지 즐겨찾기