williamsandonz
williamsandonz

Reputation: 16420

Hibernate, force get objects from Database

If I add a new object and go .save() then run a select, the last object created, is retrieved from the classes, rather than the DB, which means a date property hasn't been converted into SQL format and its mucking up some frontend code of mine.

E.G

Tue Feb 06, 2012 instead of 2012-02-06 00:00:00

Is there a way to make sure all my objects get pulled from database?

Thanks

Upvotes: 0

Views: 2938

Answers (2)

manocha_ak
manocha_ak

Reputation: 902

by default your get() or load will read from Cache first and then go to database if not found in database. On which it also get added to cache. You will need session.get() with appropraite LockMode. Not the default LockMode.NONE.

Plus I believe it requires flush() also in between save() and get() to gaurantee that save operation has been synchronized to database.

Upvotes: 0

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

Hibernate has a few levels of cache. What you are experiencing is the session/first level cache. There is no way to disable that. But you can look into using a StatelessSession http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/batch.html Section 15.3 if you are insistent on doing something like this.

I advice you re-think your separation of User Interface, Business Logic and Persistence Layer if you are relying on the database to return correctly formatted date strings to your users. You shouldn't care how the Database or Hibernate formarts strings as long as you can understand the data you are storing/retrieving. Look into MVC, MVVM, or any other patterns of separation.

Upvotes: 2

Related Questions