Is the below statement a valid one?
persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries
When I try the below code using persist; then the row is getting inserted without any transaction (It is commented out).
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();
This statement is correct. When control returns from
persist()back to your code, noINSERTstatements have been executed. These statements are guaranteed to be deferred until session flushing. Note thatpersist()would be a pointless method if no insert happened ever.