对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语句,这是为啥?
我想我可能理解的不对,请有经验的朋友指点一下,谢谢了

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

[quote]您好,看了您的博客,我也是初学,想请教您个问题,详见http://www.iteye.com/problems/12480
我也看了ytsunwei 的回答,但是我在Eclipse中没有添加事务代码,执行Save后仍然没生成任何sql语句,恳请您指点指点,谢谢您了[/quote]

回复:
我在我本地测试了一下, save不在事务中时,所以它会立即执行Sql insert.

[code="java"]
session = HibernateUtil.getSeesion();
final Transaction tx = session.beginTransaction();
tx.commit();
session.save(depart); //先插入部门
session.save(em1); //后插入员工
session.save(em2);
[/code]

控制台输出了SQL:
Hibernate: insert into Department (name) values (?)
Hibernate: insert into Employee (name, depart_id) values (?, ?)
Hibernate: insert into Employee (name, depart_id) values (?, ?)