hibernate中的session.flush和commit的区别
1个回答
展开全部
参考地址:http://blog.csdn.net/cmtobby/article/details/1750632
保存:
[java] view plain copy
session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
①执行save()方法,不执行session.flush()方法,或者commit()方法:只有选取出主键Id字段
[java] view plain copy
Hibernate: select hibernate_sequence.nextval from dual
[java] view plain copy
</pre><span style="color:#ff0000">②</span>执行save(),并且<span style="color:#ff0000">执行flush()方法</span>:向缓存提交插入语句<p></p><p></p><pre name="code" class="java">session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
session.flush();
所以有提交,不执行
[java] view plain copy
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into SXBBKF.J_CARS (NAME, TIME, ID) values (?, ?, ?)
③执行save()方法并commit()
[java] view plain copy
session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
session.getTransaction().commit();
提交了插入sql语句,并执行了更新
[java] view plain copy
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into SXBBKF.J_CARS (NAME, TIME, ID) values (?, ?, ?)
附:关于beginSession问题
一、在save()方法中:
[java] view plain copy
session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
session.getTransaction().commit();
开启事务,并进行事务提交
二、在delete()方法中:
[java] view plain copy
session=HibernateSessionFactory.getSession();
//session.beginTransaction();
JCars jc=(JCars)session.get(JCars.class, new Integer("104")); //get、load之后,就编程了持久化状态
session.delete(jc);
session.getTransaction().commit();
不beginSeesion,仅通过Get方法来加载
[java] view plain copy
<span style="color:#ff0000;">Transaction not successfully started</span>
并无法正常提交,会报错
开始transaction后,执行删除操作成功!
三、在modify()方法中:
[java] view plain copy
session=HibernateSessionFactory.getSession();
JCars jc=(JCars)session.get(JCars.class, new Integer("111"));
jc.setName("byd2");
session.getTransaction().commit();
同样出错:
[java] view plain copy
<span style="color:#ff0000;">Transaction not successfully started</span>
同样需要begginSession()
开启后,成功修改
保存:
[java] view plain copy
session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
①执行save()方法,不执行session.flush()方法,或者commit()方法:只有选取出主键Id字段
[java] view plain copy
Hibernate: select hibernate_sequence.nextval from dual
[java] view plain copy
</pre><span style="color:#ff0000">②</span>执行save(),并且<span style="color:#ff0000">执行flush()方法</span>:向缓存提交插入语句<p></p><p></p><pre name="code" class="java">session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
session.flush();
所以有提交,不执行
[java] view plain copy
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into SXBBKF.J_CARS (NAME, TIME, ID) values (?, ?, ?)
③执行save()方法并commit()
[java] view plain copy
session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
session.getTransaction().commit();
提交了插入sql语句,并执行了更新
[java] view plain copy
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into SXBBKF.J_CARS (NAME, TIME, ID) values (?, ?, ?)
附:关于beginSession问题
一、在save()方法中:
[java] view plain copy
session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(jc);
session.getTransaction().commit();
开启事务,并进行事务提交
二、在delete()方法中:
[java] view plain copy
session=HibernateSessionFactory.getSession();
//session.beginTransaction();
JCars jc=(JCars)session.get(JCars.class, new Integer("104")); //get、load之后,就编程了持久化状态
session.delete(jc);
session.getTransaction().commit();
不beginSeesion,仅通过Get方法来加载
[java] view plain copy
<span style="color:#ff0000;">Transaction not successfully started</span>
并无法正常提交,会报错
开始transaction后,执行删除操作成功!
三、在modify()方法中:
[java] view plain copy
session=HibernateSessionFactory.getSession();
JCars jc=(JCars)session.get(JCars.class, new Integer("111"));
jc.setName("byd2");
session.getTransaction().commit();
同样出错:
[java] view plain copy
<span style="color:#ff0000;">Transaction not successfully started</span>
同样需要begginSession()
开启后,成功修改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询