Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By Tolls

Thread: Will HQL query use Hibernate Secondary cache

  1. #1
    Sanush Chacko is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Will HQL query use Hibernate Secondary cache

    I would like to clarify some points regrading the secondary level cache of hibernate. The point to clarify is, will the HQL queries always hit the database (at least for getting ids).

    Consider we have entities

    class Customer {

    long id; // Primary key

    String name;

    set addressList; // One to many relationship

    }

    class Address{

    long id; // Primary key

    String houseName;

    }

    The database table for the Address has a foreign key reference to the Customer (id) to support one to many relationship.

    As a precondition, i have enabled the level 2 cache for hibernate as EHcache. Only the entity and associations are set to be cacheable. Query caching is not enabled. I know that if i use the session.get() or session.load() more than one time, only the first call will fire a query to the databases and subsequent ones will take the data from level 2 cache.

    My questions are

    1) Will HQL take advantage of the second level cache. In one session i executed one HQL to get the object using the primary key (id), "from Customer c where c.id = ? ").setParameter(1, 1005).

    If i ran the same HQL in a different session, will the Customer object is taken from the level 2 cache or it will hit the database again.

    2) Consider another HQL executed "from Customer as c left join fetch c.addressList" for selecting the customer and associated Address.

    If i ran the same HQL in a different session, will the associated Address be taken from the level 2 cache or it will hit the database again.

  2. #2
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: Will HQL query use Hibernate Secondary cache

    In both cases the objects will be taken from the second level cache and it won't hit database because, second level cache is stored in the SessionFactory. Only the first level cache is stored in the Session.

  3. #3
    Sanush Chacko is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Re: Will HQL query use Hibernate Secondary cache

    If the entity is retrieved using 'get' or 'load' method, we can see that the query will be executed only once. For the second time when we use the 'get' or 'load', the data from the second level cache is taken. But if we use HQL directly it will hit the DB all time. We can see this by analyzing the select queries which are shown by enabling the 'show-sql' option.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Will HQL query use Hibernate Secondary cache

    I can indeed imagine that at least a query needs to be executed to know which specific entities to fetch from the cache. Does the HQL path also cause SQL queries for lazy fetched data?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Will HQL query use Hibernate Secondary cache

    Stack Overflow.
    And another one.

    Essentially, unless you have query caching (and the query matches exactly), a query will go to the db to. It will then use the IDs returned to try and grab the entity off the session cache (or possibly second level cache?). So whatever happens you'll always see a query, what you won't necessarily see (depends on logging I expect) is whether the resultant entity is from the cache or not.
    gimbal2 and Sanush Chacko like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Sanush Chacko is offline Member
    Join Date
    Jun 2014
    Posts
    3
    Rep Power
    0

    Default Re: Will HQL query use Hibernate Secondary cache

    @Tolls thanks for the clarification.

Similar Threads

  1. Database Query using Hibernate
    By javababy in forum Web Frameworks
    Replies: 0
    Last Post: 08-02-2011, 11:54 AM
  2. am little confused about the cache levels in hibernate
    By siri.th in forum Advanced Java
    Replies: 1
    Last Post: 08-04-2010, 12:50 PM
  3. Help query with hibernate
    By wonderbiker in forum JDBC
    Replies: 0
    Last Post: 04-14-2009, 11:12 AM
  4. Hibernate cache problem
    By jispat in forum New To Java
    Replies: 0
    Last Post: 12-29-2008, 11:18 AM
  5. Hibernate sql query
    By taicode in forum JDBC
    Replies: 0
    Last Post: 03-25-2008, 04:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •