Hibernate flush doesn't update database

天涯浪子 提交于 2019-12-25 01:48:32

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/471797/hibernate-flush-doesnt-update-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!