views:

395

answers:

3

Hi,
hibernateSession.createQuery("select foo where id = 1");
// This command return the Item with id 1.

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

hibernateSession.createQuery("select foo where id = 1");
// This command return the Item with id 1 too ! :-(

It's the same with hibernateSession.flush()/hibernateSession.clean()
I think I don't well use my hibernate cache...

+1  A: 

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

Mike Pone
+3  A: 

The first query will have loaded that object into the hibernate session. Your deletion of the row in the database has no impact, since you are using the same session.

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

A_M
+1  A: 

Try this

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 you're problem is with the L1 cache. The L1 cache is ALWAYS there, is associated with a given Session object and is independent of the L2 cache which is what all the hibernate cache documentation talks about. 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 r1 == r2.

Basically, using hibernate when there can be concurrent modifications to the DB is not straightforward.

Jherico