hibernate 持久化对象 session中的方法

来源:互联网 发布:智能建站软件 编辑:程序博客网 时间:2024/04/27 20:58
package cn.itcast.hibernate.state;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class SessionMethod {/** * s.saveOrUpdate(c); * 当一般对象 根据id判断是指向save还是update * 如果是游离对象 执行的是update * 如果是持久对象  不执行 * 判断临时对象的标准 * 如果Customer类中 private Integer id;定义Integer类型 *     这时id=null 此时 表示是临时对象  * 如果Customer中的属性值 private int id=4;定义为int *     这是需要在 Customer。hbm。xml文件中的的id标签中增加 *     unsaved-value=“0”属性 *        如果Customer类中id属性的值和id标签中unsaved-value的值相同 *        则为 临时对象    执行save操作 *    不相等 不是临时对象 此时执行  update操作 *  */private static SessionFactory sf;static{Configuration config=new Configuration();config.configure("cn/itcast/hibernate/state/hibernate.cfg.xml");config.addClass(Customer.class);config.addClass(Order.class);sf=config.buildSessionFactory();}//操作持久化对象@Testpublic void updatePersistClass(){/** * <class name="cn.itcast.state.Custoemr" table="custoemr"select-before-update=true * select-before-update=true 在更新之前查询 */Session s=sf.openSession();Transaction ts=s.beginTransaction();Customer c=(Customer)s.get(Customer.class, 3);s.evict(c);//将持久化对象变为游离对象/* * 原来在update会产生条update语句 因为没有意义 所以增加select-before-update=true语句 * 在更新前查询 这样sesiong的一级缓存中又有了  所以不会有update语句了 */s.update(c);ts.commit();s.close();}//操作持久化对象@Testpublic void updatePersistId(){Session s=sf.openSession();Transaction ts=s.beginTransaction();Customer c=(Customer)s.get(Customer.class, 3);/** * org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.itcast.hibernate.state.Customer#3] * 因为之前session的一级缓存中有值 变为游离对象后没有了  c2进入后  s.update(c)  * c1又进去了 c1和c2的Oid是一样的 所以会报错 */s.evict(c);//将持久化对象变为游离对象Customer c2=(Customer)s.get(Customer.class,3);s.update(c);ts.commit();s.close();}@Testpublic void updatePersistRemove(){Session s=sf.openSession();Transaction ts=s.beginTransaction();Customer c=(Customer)s.get(Customer.class,3);s.evict(c);s.update(c);/* * 如果更新是 数据库不存在响应记录 则抛出异常 * 要把select-before-update=false  要不不执行update语句 * 当变成游离对象时  session的一级缓存没有了 update的时要从数据库中获取 * 数据  如果没有变成游离的 session的缓存中有对象  从sessiong的缓存中更新 * 不会报异常 */ts.commit();s.close();}@Testpublic void saveOrupdatePersist(){Session s=sf.openSession();Transaction ts=s.beginTransaction();Customer c=new Customer();c.setName("xxxxxxx");//此时执行update/* * 既可以执行save也可以执行update  根据oid决定 */s.saveOrUpdate(c);ts.commit();s.close();}//如果是游离对象就操作update方法@Testpublic void saveOrupdateEvict(){Session s=sf.openSession();Transaction ts=s.beginTransaction();Customer c=(Customer) s.get(Customer.class,1);s.evict(c);//如果是游离对象执行updates.saveOrUpdate(c);ts.commit();s.close();}//如果是持久对象不执行任何操作@Testpublic void saveOrupdatepersistx(){Session s=sf.openSession();Transaction ts=s.beginTransaction();Customer c1=(Customer)s.get(Customer.class,1);//如果是持久对象 不执行任何操作s.saveOrUpdate(c1);ts.commit();s.close();}//测试load方法和get方法@Testpublic void testGetOrLoad(){Session s=sf.openSession();Transaction ts=s.beginTransaction();/*                 * 知识点15: 操纵持久化对象-get()  load()                 *  get和load方法区别:                 *     * 如果查询的id不存在                 *        * get方法返回null                 *        * load方法抛出异常                 *     * get方法总是立即检索                 *       load:可以配置成立即检索和延迟检索                  */Customer c8=(Customer)s.get(Customer.class,8);System.out.println(c8);ts.commit();s.close();}}

原创粉丝点击