Search This Blog

Friday, 16 August 2013

Hibernate Cache Framework

The Hibernate framework comes with a complete caching system. This cache system is used to reduce the amount of SQL queries fired in the system.
The Hibernate cache is built at two levels - a first level or session cache which we saw earlier and the second level cache. While the first level cache is non optional, the second level cache needs to be configured and switched on for use.
The first level cache works at a session scope. The second level cache on the other hand is associated with the sessionFactory. Thus it is possible for different sessions to find and use this cached data. In case of a clustered application we can have a cache that is used across multiple session factories.


 As can be seen above the Hibernate second level cache is built of multiple components.
  1. The class cache is used to cache instances of a class. The cache is used for a whole hierarchy of classes and not just a particular sub-class.
  2. Classes may have Collections within them and you would like to cache which entities are associated with a particular collection. For this the collections cache is used. For every collection that you need to be cached, you need to define a collection cache region.
  3. If we include cache settings in our mapping data, then the second level cache is automatically enabled. This is not the case with query cache. We need to explicitly enable it as a part of hibernate settings.
  4. The Query cache is a specialized cache that is used to cache the results of a query. The Query cache is not enabled by simply enabling the second level cache. To cache query results we need the following setting in our cfg files:  hibernate.cache.use_query_cache = true
  5. On enabling query caching, Hibernate sets up an additional cache holding timestamps of the most recent updates to tables against which queries are cached.
The Hibernate caching framework is flexible enough and allows us the choice of using our own Cache providers. It also provides built in support for some cache providers. I shall configure a second level cache using one of these providers - EhCache.
For this example I created three classes:
public class Book {
    private String name;
    private Integer id;
    private BookType bookType; // many books have one book Type
    private BookReleaseDetail releaseDetail; // This is a one to one relation
}

public class BookReleaseDetail {
    private Integer id;
    private Book book;
    private Date releaseDate;
    private String releasedBy;
}

public class BookType {
    private String name;
    private Set<String> descriptors = new LinkedHashSet<String>();
    private Integer id;
}
To setup the cache provider we need to make entries in our cfg file:
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
The first element is a global switch that tells Hibernate to switch on the second level caching support. The second tells Hibernate the name of the cache provider to use. If we need to disable the cache we set the value to false. You do not have to remove the caching configurations added to the class metadata.
The next step is to configure our entities to work with caching.

29 comments:

  1. Android Training in Chennai19 February 2015 at 14:10

    Awesome Blogs share more information and refer the link Android Training in Chennai

    ReplyDelete
  2. Hi, Thanks for sharing this valuable blog. I did HTML5 course at reputed HTML5 Training Institutes in Chennai. This is reslly useful for me to make a bright future in designing field.

    ReplyDelete
  3. Thanks for sharing this information. SEO is one of the digital marketing techniques which is used to increase website traffic and organic search results. If anyone wants to get SEO Course in Chennai visit FITA Academy located at Chennai. Rated as No.1 SEO Training Center in Chennai.

    ReplyDelete
  4. Hi, I am Martina from Chennai. I am technology freak. I did Android mobile application development course at Fita academy, this is very useful for me to make a bright career in IT industry. So If you looking for best Android Training Institute in Chennai please visit fita academy.

    ReplyDelete
  5. A very informative post. Helped a lot, as I used it as a reference to train my students. Thanks a bunch.
    HTML5 Course in Velachery | HTML5 Course in Velachery

    ReplyDelete
  6. I agree with your post. Android software development kit makes the application development process lot simpler and effective. You can create best performing android application with ease. Android Training Institutes in Chennai | Android Training in Chennai

    ReplyDelete
  7. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai

    ReplyDelete
  8. Thanks for the sharing useful information content
    Hi we at Colan Infotech Private Limited , a company which is Situated in US and India, will provide you best java web service and our talented
    java application development. team will assure you best result and we are familiar with international markets, We work with customers in a wide variety of sectors. Our talented team can handle all the aspects of
    Java web application development,we are the best among the
    Java development company.
    We have quite an extensive experience working with
    java development services.
    we are the only Java application development company which offer custom services to a wide range of industries by exceeding our client’s expectations. You can even interact directly with the team regarding your project, just as you would with your in-house team.Our pro team will provide you the best
    java appliaction development services.
    We are best among the
    java development companies in Chennai,
    please review our customer feedbacks so that you may find a clue about us. If you want one stop solution for java development outsourcing, Colan infotech is the only stop you need to step in. Colan Infotech is the unique
    java web development company.were our team of unique
    java application developer
    were ranked top in
    java enterprise application development.

    ReplyDelete
  9. Thanks for sharing useful information article to us keep sharing this info,
    Hi We at Colan Infotech Private Limited a
    Mobile application development company in chennai,
    is Situated in US and India, will provide you best service in
    enterprise mobile app development company .
    and Colan Infotech has a group of exceedingly dedicated, inventive and creative experts with an energy for delivering exciting , helpful and stylish Web and Mobile Applications, We work with customers in a wide variety of sectors.
    We design all of our websites and applications using the responsive web design approach. Our talented team can handle all the aspects of mobility so we are rated as best service provider in
    Mobile apps development companies in chennai.
    We solidly trust that our customers start things out and there is not a viable alternative for quality of service.
    We offer custom services to a wide range of industries by exceeding our client’s expectations. You can even interact directly with the team regarding your project, just as you would with your in-house team. we always desire to solicit our customer's fruitful experience with us, we are the top notch
    Mobile App Development Company in chennai
    and mobile app development companies in Bangalore. We can provide best
    mobile app development chennai .
    We can provide cutting edge technology services in
    Mobile application development in chennai.
    Reach us for mobile app development chennai or just call us for best
    mobile app developers in chennai .

    ReplyDelete


  10. This article is helpful to know more about the Android and keep on updating us the information. This is more informatics and it really helped me to know the Android.
    Android Training in Chennai | Android Course in Chennai | Android Training Chennai

    ReplyDelete
  11. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
    please sharing like this information......
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  12. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
    please sharing like this information......
    Web Development Company

    ReplyDelete

  13. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Web Design Company
    Web Development Company

    ReplyDelete
  14. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  15. Nice Posting..

    Useful topic hibernate cache framework ...

    Red Hat Training in Chennai

    ReplyDelete

  16. Hai Author, Very Good informative blog post,
    Thanks for Sharing

    ReplyDelete
  17. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    best java training in chennai |
    Core java training in chennai

    ReplyDelete
  18. Java is high level programming language .Due to its stability and scalability, you can find Java on mobiles, desktops, large scale applications etc. Java is also gaining big in the field of Internet of Things (IoT).
    Java training institute in chennai

    ReplyDelete
  19. Java is high level programming language .Due to its stability and scalability, you can find Java on mobiles, desktops, large scale applications etc. Java is also gaining big in the field of Internet of Things (IoT).
    Java training institute in chennai

    ReplyDelete
  20. nice blog. thanks for sharing valuable information. It’s really good.Any one looking software courses nareshit is the best offline and online software training institute in Hyderabad and Chennai.

    ReplyDelete
  21. I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post. Thanks
    wordpress web design

    ReplyDelete