Hibernate - same result after update / select

hibernateSession.createQuery ("select foo where id = 1");
// This command returns the item with id 1.


// [BREAK POINT STOP] ==> I am going to MySQL and I delete this item manually.
// [BREAK POINT CONTINU]


hibernateSession.createQuery ("select foo where id = 1");
// This command returns item ID 1 too! :-(


This is the same with hibernateSession.flush () / hibernateSession.clean ()
I guess I am not using my hibernate cache ...

0


a source to share


3 answers


The first request will load this object into a hibernate session. Your deletion of a row in the database has no effect as you are using the same session.



You either need to start a new session or evict an object from the session.

+3


a source


Definitely a caching issue. Are you using the same session? Try closing the session and getting a new one from the factory.



+1


a source


try it

Object o = hibernateSession.createQuery("select foo where id = 1").uniqueResult();

// [BREAK POINT STOP] ==> I go in MySQL and I delete this item manualy.

hibernateSession.evict(o);
hibernateSession.createQuery("select foo where id = 1");

      

If that works, then the problem is with the L1 cache. The L1 cache is ALWAYS there, associated with the given Session object and does not depend on the L2 cache, which is what all the hibernation cache documentation says. The purpose of the L1 cache is to satisfy the requirement that if you get the same database object twice in the same session, the two references will satisfy the requirement r1 == r2.

In principle, using hibernation when concurrent changes to the database are possible is not easy.

+1


a source







All Articles