How do I prevent Spring/Hibernate from automatically committing my modifications to the database?
I am working with Spring and Hibernate. I have an object which was retrieved from the database with HibernateTemplate.
When I modify this object Hibernate is making inserts into the database before the data is ready to be inserted, the result is a lot of database errors along the line of "cannot insert NULL into ...".
Is there a way to tell Spring/Hibernate "don't update the database with this until I call HibernateTemplate.persist()"? I lo开发者_开发技巧oked in the HibernateTemplate javadoc but couldn't find anything
Hibernate is flushing changes because you use transaction scoped persistence context. That means that all managed entities are synchronized with database when the transaction commits. If you don't like it, then simply don't make your method a transactional one. This way entity you get will not be synchronized with database - there wont be any transaction commit.
Try annotate the method, in which you don't want any modifications like:
@Transactional(readOnly = true)
public yourMethod(){
//some hibernate-spring code here
}
精彩评论