benhsu
benhsu

Reputation: 5546

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 looked in the HibernateTemplate javadoc but couldn't find anything

Upvotes: 1

Views: 1914

Answers (2)

Gennady Nikolaev
Gennady Nikolaev

Reputation: 81

Try annotate the method, in which you don't want any modifications like:

@Transactional(readOnly = true)
public yourMethod(){
    //some hibernate-spring code here
}

Upvotes: 0

Pawel Szulc
Pawel Szulc

Reputation: 1071

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.

Upvotes: 1

Related Questions