hibernate merge saveOrUpdate的区别

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.anyec.make.bo.ModelBO;
import com.anyec.make.domain.Model;

@Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class HibernateTest {
	private ModelBO modelBO;

	public ModelBO getModelBO() {
		return modelBO;
	}
	@Resource
	public void setModelBO(ModelBO modelBO) {
		this.modelBO = modelBO;
	}
	@Test
	public void testMerge(){
		Model model=new Model();
		model.setCnName("test");
		model.setName("testName");
		model.setPackName("testPackName");
		System.out.println(JSON.toJSONString(model));
		modelBO.merge(model);//model的ID不会变
		System.out.println(JSON.toJSONString(model));
		/***
		 * {"children":[],"cnName":"test","id":0,"name":"testName","packName":"testPackName"}
		 * Hibernate: insert into Model (cnName, name, packName) values (?, ?, ?)
		 * */
		model=modelBO.merge(model);//返回被持久化的对象		
		System.out.println(JSON.toJSONString(model));
		/***
		 * {"children":[],"cnName":"test","id":0,"name":"testName","packName":"testPackName"}
		 * Hibernate: insert into Model (cnName, name, packName) values (?, ?, ?)
		 */
		modelBO.merge(model);//持久化对象,先执行select没有发生改变,所以不会被update 
		System.out.println(JSON.toJSONString(model));
		/***
		 * {"children":[],"cnName":"test","id":29,"name":"testName","packName":"testPackName"}
		 * Hibernate: select model0_.id as id5_1_, model0_.cnName as cnName5_1_, model0_.name as name5_1_, model0_.packName as packName5_1_, children1_.modelId as modelId3_, children1_.id as id3_, children1_.id as id4_0_, children1_.btw as btw4_0_, children1_.cnName as cnName4_0_, children1_.editor as editor4_0_, children1_.len as len4_0_, children1_.modelId as modelId4_0_, children1_.name as name4_0_, children1_.refId as refId4_0_, children1_.relation as relation4_0_, children1_.sqlName as sqlName4_0_, children1_.type as type4_0_ from Model model0_ left outer join Field children1_ on model0_.id=children1_.modelId where model0_.id=?
		 * {"children":[],"cnName":"test","id":29,"name":"testName","packName":"testPackName"}
		 */
		
		model.setCnName("cnName78");
		modelBO.merge(model);//持久化对象被改变,执行update 
		System.out.println(JSON.toJSONString(model));
		/**
		 * Hibernate: select model0_.id as id5_1_, model0_.cnName as cnName5_1_, model0_.name as name5_1_, model0_.packName as packName5_1_, children1_.modelId as modelId3_, children1_.id as id3_, children1_.id as id4_0_, children1_.btw as btw4_0_, children1_.cnName as cnName4_0_, children1_.editor as editor4_0_, children1_.len as len4_0_, children1_.modelId as modelId4_0_, children1_.name as name4_0_, children1_.refId as refId4_0_, children1_.relation as relation4_0_, children1_.sqlName as sqlName4_0_, children1_.type as type4_0_ from Model model0_ left outer join Field children1_ on model0_.id=children1_.modelId where model0_.id=?
		 * Hibernate: update Model set cnName=?, name=?, packName=? where id=?
		 * {"children":[],"cnName":"cnName78","id":29,"name":"testName","packName":"testPackName"}
		 */
	}
	@Test
	public void testSaveOrUpdate(){
		Model model=new Model();
		model.setCnName("test");
		model.setName("testName");
		model.setPackName("testPackName");
		System.out.println(JSON.toJSONString(model));
		modelBO.saveOrUpdate(model);//执行INSERT,并且model被持久化了,id被赋值,难怪这个方法返回的是void 
		/**
		 * {"children":[],"cnName":"test","id":0,"name":"testName","packName":"testPackName"}
		 * Hibernate: insert into Model (cnName, name, packName) values (?, ?, ?)
		 */
		System.out.println(JSON.toJSONString(model));//打印有ID
		/***
		 * {"children":[],"cnName":"test","id":30,"name":"testName","packName":"testPackName"}
		 */
		modelBO.saveOrUpdate(model);//执行了update 
		System.out.println(JSON.toJSONString(model));
		/**
		 * Hibernate: update Model set cnName=?, name=?, packName=? where id=?
		 * {"children":[],"cnName":"test","id":30,"name":"testName","packName":"testPackName"}
		 */
	}
	/**
	 * 两者都会进行update或者save操作 不同的是是 
	 * saveOrupdate如果已经在session中都会update, 并且原 实例对象会被持久化,特征是ID会变 
	 * 而 merge在不存在的时候会执行保存,但是原实例对象与被持久化的对象是2个对象,及源对象的ID不会被加载成持久化对象的ID
	 */

}

你可能感兴趣的:(hibernate merge saveOrUpdate的区别)