hibernate的save(),saveOrUpdate(),update(),merge()方法的使用

package com.dada.hibernate;

import java.util.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class TestStudent {
private static SessionFactory sf;
	
	//
	@BeforeClass
	public static void beforeClass() {
		sf = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	
	@AfterClass
	public static void afterClass() {
		sf.close();
	}
	
	@Test
	public void testSchemaExport() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}
	
	@Test
	public void testTeacherSave() {
		Teacher t = new Teacher();
		t.setAge(23);
		t.setName("dada");
		t.setBirthday(new Date());
		t.setGender(Gender.MALE);
		t.setGood(true);
		t.setYourWifeName("no body");
		
		Session session =  sf.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		
		Session session2 = sf.getCurrentSession();
		System.out.println(session == session2);
		session.getTransaction().commit();
		
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
		
		Session session3 = sf.getCurrentSession();
		session3.beginTransaction();
		System.out.println(session == session3);
		session3.save(t);
		session3.getTransaction().commit();
	}
	
	@Test
	public void testSaveWith3States() {
		Teacher t = new Teacher();
		t.setName("t1");
		t.setAge(23);
		t.setBirthday(new Date());
		t.setGender(Gender.FEMALE);
		t.setGood(false);
		t.setYourWifeName("dandan");
		
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		System.out.println(t.getId());
		t.setAge(35);
		session.getTransaction().commit();
		System.out.println(t.getId());
		
	}
	
	@Test
	public void testDelete() {
		Teacher t = new Teacher();
		t.setName("delete");
		t.setAge(23);
		t.setBirthday(new Date());
		t.setGender(Gender.FEMALE);
		t.setGood(false);
		t.setYourWifeName("dandan");
		
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		System.out.println("保存提交之前"+t.getId());
		t.setAge(35);
		session.getTransaction().commit();
		
		Session session2 = sf.getCurrentSession();
		session2.beginTransaction();
		session2.delete(t);
		System.out.println("删除提交之前:"+t.getId());
		session2.getTransaction().commit();
		
		System.out.println("删除提交之后的Name属性"+t.getName());
		System.out.println("删除提交之后的ID属性"+t.getId());
		
		Session session3 = sf.getCurrentSession();
		session3.beginTransaction();
		session3.save(t);
		session3.getTransaction().commit();
	}
	
	/**
	 * 这个打印出来的类的名称是一个代理类的名称
	 * class com.test.hibernate.Teacher_$$_javassist_1
	 */
	@Test
	public void testLoad() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher) session.load(Teacher.class, 1);
		session.getTransaction().commit();
		System.out.println(t.getClass());
	}

	/**
	 * 打印出来的是类的名称
	 * class com.test.hibernate.Teacher
	 */
	@Test
	public void testGet() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher) session.get(Teacher.class, 1);
		session.getTransaction().commit();
		System.out.println(t.getAge());
		System.out.println(t.getClass());
	}
	
	/**
	 * 一个对象的属性在被修改之后如果之后还有其他的session进行update的操作那么操作会被记录到数据库的
	 */
	@Test
	public void update1() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher) session.get(Teacher.class, 1);
		session.getTransaction().commit();
		
		t.setName("name update");
		
		Session session1 = sf.getCurrentSession();
		session1.beginTransaction();
		session1.update(t);
		session1.getTransaction().commit();
		
	}
	
	/**
	 * 要更新一个对象,必须要指定它的主键,否则更新就会出错的。
	 */
	@Test
	public void update2() {
		Teacher t = new Teacher();
		t.setAge(23);
		t.setName("kk");
		t.setBirthday(new Date());
		t.setGender(Gender.MALE);
		t.setGood(true);
		t.setYourWifeName("no body");
		
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		session.update(t);
		session.getTransaction().commit();
	}
	
	@Test
	public void update3() {
		Teacher t = new Teacher();
		t.setAge(23);
		t.setName("kk");
		t.setBirthday(new Date());
		t.setGender(Gender.MALE);
		t.setGood(true);
		t.setYourWifeName("no body");
		t.setId(1);
		
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		session.update(t);
		session.getTransaction().commit();
	}
	
	/**
	 * 在同一session内属性发生了改变会被同步到数据库的
	 */
	@Test
	public void update4() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher)session.get(Teacher.class, 1);
		t.setName("zhangsan2");
		session.getTransaction().commit();
	}
	
	@Test
	public void update5() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher)session.get(Teacher.class, 1);
		t.setName("zhangsan5");
		session.getTransaction().commit();
		
		t.setName("z4");
		
		Session session2 = sf.getCurrentSession();
		session2.beginTransaction();
		session2.update(t);
		session2.getTransaction().commit();
	}
	
	@Test
	public void update6() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("update Teacher t set t.name='tongtong' where t.id=1 ");
		query.executeUpdate();
		session.getTransaction().commit();
	}
	
	@Test
	public void update7() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		
		
		session.getTransaction().commit();
	}
	
	@Test
	public void testTpdateOrSave() {
		Teacher t = new Teacher();
		t.setName("t1");
		
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(t);
		
		session.getTransaction().commit();
		
		t.setName("t2");
		
		Session session2 = sf.getCurrentSession();
		session2.beginTransaction();
		session2.saveOrUpdate(t);
		session2.getTransaction().commit();
	}
	
	@Test
	public void testClear() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher)session.load(Teacher.class, 1);
		System.out.println(t.getName());
		
		session.clear();
		
		Teacher t2 = (Teacher)session.load(Teacher.class, 1);
		t2.setAge(7);
		System.out.println(t.getAge());
		//这种情况下虽然看似一个session中拥有了两个完全一样的对象,但是t已经被清除了,
		//所以不存在有两个相同对象存在于同一个session中的情况。
		System.out.println(t2.getName());
		session.update(t2);
		session.getTransaction().commit();
	}
	
	@Test
	public void testFlush() {
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher)session.load(Teacher.class, 1);
		t.setName("tttt");
		
		//如果没有调用clear()方法,那么结果就是会多一句update语句
		//如果调用了clear()方法,那么session就会被清空,
		//所以对象就和session没有关系了,因此就不存在session要把对象更新到数据库中的情况了
		//session.clear();
		
		t.setName("t2222");
		
		session.getTransaction().commit();
	}
	
	
	/**
	 * 在同一个session里面如果出现了两个相同的托管对象,那么这个时候保存的时候就要使用merge()方法
	 */
	@Test
	public void testMerge() {
		
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Teacher t = (Teacher) session.get(Teacher.class, 1);
		System.out.println("保存提交之前"+t.getId());
		t.setAge(55);
		session.getTransaction().commit();
		t.setAge(88);
		
		Session session2 = sf.getCurrentSession();
		session2.beginTransaction();
		Teacher t1 = (Teacher) session2.get(Teacher.class, 1);
		t1.setAge(99);
		//session2.update(t);
		
		session2.merge(t1);
		session2.getTransaction().commit();
	}
	
	
	
	public static void main(String[] args) {
		new TestStudent().testSchemaExport();
	}
}

你可能感兴趣的:(45_hibernate)