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.

91 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
  22. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up salesforce Online Training Bangalore

    ReplyDelete
  23. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.

    DevOps Training in Chennai

    ReplyDelete
  24. Thanks for sharing valuable information through this blog..
    Visit Best IoT Training Classes in Jaipur

    ReplyDelete
  25. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up selenium Online Training Bangalore

    ReplyDelete
  26. Extraordinary and helpful article. Making content consistently is extremely intense. Your focuses are roused me to proceed onward.


    digital marketing

    ReplyDelete
  27. I appreciate what you folks are as a rule up as well. This kind of astute work and scope! Keep up the brilliant works folks I've added you all to my blog roll.


    ABiNitio online training in Hyderabad

    ABiNitio training in Hyderabad

    online ABiNitio training in Hyderabad

    ReplyDelete
  28. After I read and try to understand this article in conclusion amazingwe are generally grateful for the nearness of this article can incorporate impressively more learning for each one of us. thankful to you.


    Accountants Brighton

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. 6-week summer course in Noida - 6 weeks The summer course plays a crucial role in shaping the career of young aspiring / informatics students. This training has been specifically introduced so that students can become familiar with current industrial culture and industrial needs. Webtrackker technology offers a 6-month training program for students / graduates that includes small and large projects.

    6-week summer course in Noida

    ReplyDelete
  31. I've been surfing on the web over 3 hours today, yet I never found any fascinating article like yours. It's enough worth for me. As I would see it, if all web proprietors and bloggers made exceptional substance as you did, the net will be basically more productive than at whatever point in late memory.

    Brighton Accountants

    ReplyDelete
  32. I've been surfing on the web more than 3 hours today, yet I never found any stupefying article like yours. It's imperatively worth for me. As I would see it, if all web proprietors and bloggers made confusing substance as you did, the net will be in a general sense more profitable than at whatever point in late memory.

    Tax Advisors

    ReplyDelete
  33. Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.

    AutoCAD training in Noida

    AutoCAD training institute in Noida


    Best AutoCAD training institute in Noida

    ReplyDelete
  34. thank you for sharing your blog information. visit us at
    Hibernate Training in Austin

    ReplyDelete
  35. Video editing course in Noida
    Video editing training institute in Noida- Webtrackker Technology is and IT Training institute providing the Video editing course in Noida, FCP, Final Cut Pro Training in Noida. For more call us- 8802820025.
    Video editing course in Noida
    Company Address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025
    Email: info@webtrackker.com
    Website: http://webtrackker.com/Best-training-institute-Video-editing-FCP-course-in-Noida.php

    ReplyDelete
  36. Cloud Computing Training In Noida
    Webtrackker is IT based company in many countries. Webtrackker will provide you a real time projects based training on Cloud Computing. If you are looking for the Cloud computing training in Noida then you can join the webtrackker technology.
    Cloud Computing Training In Noida
    Company Address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Email: info@webtrackker.com
    Website: www.webtrackker.com
    http://webtrackker.com/Cloud-Computing-Training-Institutes-In-Noida.php

    ReplyDelete
  37. The way you have expressed your thoughts in this blog was nice with clear explanation. Thanks for sharing.
    AutoCAD Training in Hyderabad

    ReplyDelete
  38. Hi,

    Thank you for sharing useful article

    I would like to introduce one of Our web designers and developers will give the best and high quality local USA website Development Services with low cost if you need more details about our web designing services so please feel free to contact us.

    Ecommerce Web Development Services in USA

    Magento Ecommerce Website Development
    Travel portal Development
    Web Application Development USA
    Yalavarthi Software solutions USA

    ReplyDelete
  39. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iPhone app training course in bangalore
    iPhone job oriented course in bangalore

    ReplyDelete

  40. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your blog.
    iOS course fee in bangalore
    iPhone training classes in bangalore

    ReplyDelete

  41. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...
    ABiNitio online training in Hyderabad
    ABiNitio training in Hyderabad
    Best ABiNitio online training in Hyderabad

    ReplyDelete
  42. This comment has been removed by the author.

    ReplyDelete
  43. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your blog.
    ios app development course
    iPhone training institute in bangalore

    ReplyDelete
  44. click here for more information about the latest stuff oneplus c type jack

    ReplyDelete
  45. Like different elements of India, numerous oil and spices usually cross into making food. This effects in substances getting caught to the partitions of the filter out.
    Visit here
    http://kitchenware.ml
    Best kitchen chimney installation and service
    Auto clean chimney sevice in Faridabad

    ReplyDelete
  46. Thanks for Sharing this blog useful content and more informative.

    Blockchain Online Training

    ReplyDelete
  47. Nice post. Thanks for sharing wonderful information.
    Digital marketing course in Chennai

    ReplyDelete
  48. Wow, Nice blog. Thank you so much for the efforts of this blog. Visit for
    Hong Kong Honeymoon Packages

    ReplyDelete
  49. Hi,

    Thanks for sharing this knowledgeable content. I like this because it helps me a lot. Please update more content so that it can help me a lot in further studies.

    For more details to visit: Accounting Training.

    ReplyDelete

  50. Thanks you for sharing this unique useful information content with us.
    DevOps Online Training

    ReplyDelete
  51. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iPhone app training course in bangalore
    Mobile app training institutes bangalore
    iPhone job oriented course in bangalore
    iPhone training classes in bangalore

    ReplyDelete
  52. Information from this blog is very useful for me, am very happy to read this blog Kindly visit us @ Luxury Watch Box | Shoe Box Manufacturer | Luxury Cosmetics Box

    ReplyDelete