pdhinoja
pdhinoja

Reputation: 95

what is difference between hibernateTemplate Flush() and clear() methods?

what is difference between hibernateTemplate flush() and clear() method? which one to use when?

I have following code

    public void saveAllReportRoomRes(List<ReportRoomRes> reportRoomResList) throws  DataAccessException {
    hibernateTemplate.saveOrUpdateAll(reportRoomResList);
    hibernateTemplate.flush();
    }

I call this method in loop which passes list of records(total records can be more than 2 lacs) each time. because my code is under transaction, sometime in subsequent iteration, if hibernate found same primary key (I have composite primary key) object, it is throwing NonUniqueObject Exception. but actually it needs to update the record which was inserted before.

If I user HibernateTemplate.clear(), it is working.

Also is it good way to save or Update large amount of records? as this is my usual case where I am copying data from one DB to another DB at certain interval.

Upvotes: 3

Views: 8817

Answers (1)

Sireesh Yarlagadda
Sireesh Yarlagadda

Reputation: 13736

Hibernate Session.clear() :

void org.hibernate.Session.clear()

Remove all objects from the Session cache, and cancel all pending saves, updates and deletes.

Where as Session.flush Flush all pending saves, updates and deletes to the database.

http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html

Picking the right one, depends upon the transaction type, you choose.

Answering to one more question

Use Batch for updating huge records.

Upvotes: 2

Related Questions