martini69290
martini69290

Reputation:

Hibernate - Same result after update/select

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...

Upvotes: 0

Views: 2502

Answers (3)

Jherico
Jherico

Reputation: 29240

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.

Upvotes: 1

A_M
A_M

Reputation: 7851

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.

Upvotes: 3

Mike Pone
Mike Pone

Reputation: 19330

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

Upvotes: 1

Related Questions