views:

1940

answers:

2

I'm using hibernate to store a set of objects from a web service.

As the object are received each I am saving them using hibernate.

Receiving the objects is wrapped in a transaction and all the objects appear in the database after the final object is received.

I am now trying have each object appear in the database when saved. I've tried to achieve this with

getHibernateTemplate().saveOrUpdate( foo );

getHibernateTemplate().flush();
getHibernateTemplate().clear();

My understanding is this should remove the values hibernate's cache and write the values to the database.

Any learning or directions?

A: 

If you're still inside a transaction then only the session or connection that opened the transaction will be able to see the records. In some databases, you should see them from another session if you do a dirty/uncommitted read. I would try running a select using the same Hibernate session after the flush to verify that it really is in the database. Just don't query by the primary key or you may get it from the cache.

Brian Deterling
A: 

Thanks for the help Brian. The problems turned out to be a for loop in another class wrapping the save call.

The solution was to remove the for loop and replace it with an iterator.

Hibernate was keeping the same transaction for the entire for loop. Using the iterator, Hibernate seems to start a new transaction and hence performs the commit to the database and then a flush before beginning the next transaction.

sre