Hibernate簡單的幾個測試流程

package Test;

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

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.tedu.entity.Book;
import com.tedu.entity.Student;
import com.tedu.entity.Teacher;
import com.tedu.entity.User;

public class TestCase {
		private SessionFactory sf ;
		//測試將臨時狀態數據保存並轉爲持久狀態數據
		@Test
		public void Test1(){
			createSF();
			Session session = sf.openSession();
			Student s = new Student("劉維豐","16");
			s.setName("劉磊");
			//開啓事務
			Transaction t = session.beginTransaction();
			System.out.println(s);
			t.begin();
			session.save(s);
			System.out.println(s);
			t.commit();
			session.close();
		}
		//測試將臨時狀態數據保存並轉爲持久狀態數據
		@Test
		public void Test2(){
			createSF();
			Session session = sf.openSession();
			Teacher t = new Teacher("薛兆豐");
			Transaction transaction = session.beginTransaction();
			System.out.println(t);
			transaction.begin();
			session.save(t);
			transaction.commit();
			System.out.println(t);
			session.close();
			
		}
		//測試將持久狀態數據進行修改
		@Test
		public void Test3(){
			createSF();
			Session session = sf.openSession();
			Transaction transaction = session.beginTransaction();
			transaction.begin();
			Student s = (Student) session.get(Student.class, 1);
			System.out.println(s);
			s.setName("Bill");
			System.out.println(s);
			transaction.commit();
			session.close();
		}
		//測試將持久狀態數據轉爲遊離狀態數據再轉爲持久狀態而且修改
		@Test
		public void Test4(){
			createSF();
			Session session = sf.openSession();
			Transaction transaction = session.beginTransaction();
			transaction.begin();
			Student s = (Student) session.get(Student.class, 1);
			System.out.println(s);
			//將session轉換爲遊離狀態
			session.evict(s);
			//解除遊離狀態
			session.update(s);
			s.setName("Bill");
			System.out.println(s);
			transaction.commit();
			session.close();
		}
		//測試將持久狀態數據轉爲遊離狀態數據再轉爲臨時狀態
		@Test
		public void Test5(){
			createSF();
			Session session = sf.openSession();
			Transaction transaction = session.beginTransaction();
			transaction.begin();
			Student s = (Student) session.get(Student.class, 3);
			System.out.println(s);
			//將session轉換爲遊離狀態
			session.evict(s);
			//直接將數據轉爲臨時狀態(即刪除)
			session.delete(s);
			s.setName("Bill");
			System.out.println(s);
			transaction.commit();
			session.close();
		}
		//測試HQL數據
		@Test
		public void Test6(){
			createSF();
			Session session = sf.openSession();
			String HQL =  "from Student where name like '%劉%'";
			Query query = session.createQuery(HQL);
			//用query對象查找數據
			List<Student> list = query.list();
			for(Student s : list){
				System.out.println(s);
			}
			session.close();
		}
		//測試多對一和一對多的關係以及懶加載
		@Test
		public void TestOneToMany(){
			createSF();
			Session session = sf.openSession();
			User user = (User) session.get(User.class, 1);
			System.out.println(user.getId());
			System.out.println(user.getName());
			Set<Book> book = user.getBook();
			System.out.println(book.size());
			for (Book b : book){
				System.out.println(b);
			}
			session.close();
		}
		//建立SessionFactory對象
		public void createSF(){
			Configuration config = new Configuration();
			config.configure("hibernate.cfg.xml");
			sf = config.buildSessionFactory();
		}
}
相關文章
相關標籤/搜索