doraemongo 2009-03-07 22:31
浏览 406
已采纳

对Save方法和Persist方法仍然有疑问,垦盼指点

看了http://opensource.atlassian.com/projects/hibernate/browse/HHH-1273的说明,试验了一下,还是有疑问,盼望解答
为了大家方便,我把原文贴出来

引用
persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist().

persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

A method like persist() is required.

save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.

[ Show » ] Christian Bauer - 12/Oct/06 08:04 AM In case anybody finds this thread... persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist(). persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context. A method like persist() is required. save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.

引用
persist"保证",当它在一个transaction外部被调用的时候并不触发一个Sql Insert,这个功能是很有用的,
save"不保证"第2条,它要返回标识符,所以它会立即执行Sql insert,不管是不是在transaction内部还是外部
我自己编写的代码如下
    Session session1 = HibernateUtil.getSessionFactory().openSession();
Transaction tx1 = session1.beginTransaction();
tx1.commit();
Item item = new Item();
item.setItemId("101");
item.setItemName("football");
session1.save(item); //在事务外执行save方法
session1.close();
HibernateUtil.shutdown();

运行后并没有执行insert语句,这是为啥?
我想我可能理解的不对,请有经验的朋友指点一下,谢谢了
  • 写回答

2条回答 默认 最新

  • ytsunwei403 2009-03-07 22:31
    关注

    save 和 persist的唯一区别就是 在没有开启事物的条件下 ,save会产生insert语句,然后事物回滚 ,取消插入。而persist不会产生insert语句

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款