user2556304
user2556304

Reputation: 159

hibernate query result not updating after change

I am having an issue with hibernate. the query result is not updating.

i have a simple query which checks customertable to see if the 'enabled' column = true. the query works fine. but when i change the colum value from 'true' to 'false' and run the same query... it still gives me 'true' as a result.

if i close the application and recompile, and run query again, it THEN shows false, but then again if i change it back to true.. it still shows 'false' result. what am I doing wrong?

public void isEnabled(String customer){
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try{
        tx=session.beginTransaction();
        Query query = session.createQuery("FROM Customer WHERE enabled=true");
        List qr = query.list();
        boolean check = qr.iterator().hasNext();
            boolean enabled;
        sw = new ServerWriter(writer);
            if(check){
                for(Iterator itr = qr.iterator();itr.hasNext();)
                {
                    Customer c =(Customer)itr.next();
                    enabled=c.geEnabled();
                    sw.sendMessage("Customer is enabled");
                }
            }else{

                sw.sendMessage("Customer is not enabled");
            }

    } catch(HibernateException e){
        if(tx!=null){tx.rollback();}
        e.printStackTrace();
    }finally{session.close();}
}

Upvotes: 2

Views: 2180

Answers (1)

Donato Szilagyi
Donato Szilagyi

Reputation: 4369

First you forgot to close the transaction:

session.getTransaction().commit(); 

The reason you get the same value when querying second time is Hibernate cache. You always have a first level cache and if you configured it, you can have a second level cache, too.

You can refresh the first level cache before executing the query with:

session.refresh() 

If you happen to have a second level cache you can skip it with this hint:

query.setHint("org.hibernate.cacheMode", CacheMode.IGNORE);

Upvotes: 1

Related Questions