下面的陈述是有效的吗?
persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries
当我使用persist尝试下面的代码时,在没有任何事务的情况下插入了行(它被注释掉了)。
SessionFactory sessionFactory = new Configuration().configure("student.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
//Transaction tran = session.beginTransaction();
/*
* Persist is working without transaction boundaries ===> why?
*/
Student student = new Student();
student.setFirstName("xxx");
student.setLastName("yyy");
student.setCity("zzz");
student.setState("ppp");
student.setCountry("@@@");
student.setId("123456");
session.persist(student);
//tran.commit();
session.flush();
session.close();发布于 2013-01-03 20:53:53
persist()还保证,如果在事务边界之外调用INSERT语句,它将不会执行该语句
这句话是正确的。当控制权从persist()返回到您的代码时,没有执行任何INSERT语句。这些语句被保证延迟到会话刷新。注意,如果没有发生插入,那么persist()将是一个毫无意义的方法。
发布于 2013-01-03 20:44:47
https://stackoverflow.com/questions/14138955
复制相似问题