开发者

How do I prevent Spring/Hibernate from automatically committing my modifications to the database?

开发者 https://www.devze.com 2022-12-20 03:16 出处:网络
I am working with Spring and Hibernate. I have an object which was retrieved from the database with HibernateTemplate.

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消