-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Second-level cache doesn't store not found results
PostPosted: Sat Feb 20, 2016 6:05 am 
Newbie

Joined: Thu Jul 24, 2008 9:08 am
Posts: 3
Location: Poland
Can Hibernate 2LC do caching for not found records in database? I'm googling and searching Hibernate forums too but usually standard 2LC topics are coming up.

I query Account entity by primary key accountNumber - it is configured as @Cachable.
If I execute em.find('AA') first time then hibernate will check 2LC, it is missed in 2LC then SQL query will be executed. There is no account AA in DB so em.find call return null.

If I execute em.find('AA') second time in another transaction then hibernate will execute SQL query again even SQL was executed before.

Is it possible that Hibernate will save null from first executed query so another SQL is not required until AA key will be invalidated in cache?

I use EAP 6.2.4, Hibernate 4.2.7, Infinispan 5.2.7

Thanks


Top
 Profile  
 
 Post subject: Re: Second-level cache doesn't store not found results
PostPosted: Mon Feb 22, 2016 9:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
First of all, why would you go to the cache with an identifier value that doesn't exist?

Anyway, you can still get what you are asking for. All you need to do is to switch to a cacheable query:

Code:
doInJPA(entityManager -> {
    Session session = entityManager.unwrap(Session.class);
    List<Post> post = session
        .createQuery("select p from Post p where p.id = :id")
        .setParameter("id", 1L)
        .setCacheable(true)
        .list();
});


You need to enable query caching too:

Code:
hibernate.cache.use_query_cache = true


This way, the query result will be cached in the database and the second transaction will load the result from the query cache and there is no database roundtrip.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Second-level cache doesn't store not found results
PostPosted: Tue Feb 23, 2016 12:55 pm 
Newbie

Joined: Thu Jul 24, 2008 9:08 am
Posts: 3
Location: Poland
Thanks. I was thinking about using query cache here.

This is for purpose for specific/default logic with relation using two fields as primary key like Number/Category. If there is entity with Number1/Category1 then use it otherwise try to use Number1/Default. With query cache I can check all entities with Number1 without looking at category - it will work with SQL approach but I preferred using 2LC with primary key to keep my application less depended on SQL and more closer to querying by primary key. I don't have too much SQL logic and want to test it soon against 1LC without DB by changing DAOs.


Top
 Profile  
 
 Post subject: Re: Second-level cache doesn't store not found results
PostPosted: Wed Feb 24, 2016 5:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Hibernate also support searching by natural-id, so, if you can represent that querying criteria as a @NaturalId, then you can use the natural-id second level cache support.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.