• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't get 2nd level cache to work

 
Christopher Sharp
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm new to Hibernate and have been going through the tutorials at http://www.youtube.com/watch?v=Yv2xctJxE-w&index=1&list=PL4AFF701184976B25. There are 34 tutorials and 36 video, as tutorial #3 is split into three videos. Unlike in the tutorials, I'm using the later version Hibernate 4, and a different database, MySQL, with Eclipse. Apart from a few minor changes for the configuration, and some minor differences with some of the annotations, I've got essentially all the examples to work so far up to and including tutorial #32.

However, I'm having problems with Tutorial #33, and I'm unable to get the second level cache to work, if I can fix this, then tutorial #34 should also work, then I'm home and dry with this set.

I'm using Eclipse with Java 1.7, and have hibernate-core-4.0.1.Final.jar, hibernate-commons-annotations-4.0.1.Final.jar and various other jar files in my Hibernate library, and before tutorial #33 they work correctly. However, on downloading hibernate-ehcache-4.0.1.Final.jar and putting it in my Hibernate library, configuring the hibernate.cfg.xml file, and annotating one of the classes, no matter what I do, the application fails.

Specifically, this is what I'm doing in following the tutorial #33. For the main class I have:

Note that I'm using slightly different code for the session factory, as the version in the tutorial is now deprecated.

The UserDetails class is as follows:

and when the hibernate.cfg.xml files is configured with:

the code works with the 2nd level cache disabled, which is the default, and two SQL query strings are created in the main method, one for each of the two session, as expected.

After putting hibernate-ehcache-4.0.1.Final.jar in the library and trying out various ideas in the xml file, such as:

together with the annotations:

placed immediately after @Entity in the UserDetails class, when the application is run, I get the error message:

Exception in thread "main" org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]

Based on the link http://howtodoinjava.com/2013/07/04/hibernate-ehcache-configuration-tutorial/, I tried out the xml for Hibernate 3.3 and above, but this generated an error because it did not understand "key". The example in the tutorial presumably will not work as it is used with an earlier version of Hibernate.

Does anybody know how the xml file should be configured, and is this the main problem? As far as I can see the UserDetails class is correctly annotated. In this tutorial when the application is working correctly, only one SQL query is generated as the 2nd level cache is used to store the string after the 1st session is closed, but I never get as far as that because it is unable to use the 2nd level cache.

I would be very grateful if someone could kindly help me find where I have gone wrong.
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this link, especially section hibernate 4.x
http://ehcache.org/documentation/user-guide/hibernate
 
Christopher Sharp
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thanks you very much for your very useful tip. After a lot of messing around I managed to get the code to work as in tutorial #33, although I have not tried #34 yet. From the lower part of the page on the link you gave me I put in the following xml code:

after obviously commenting out the disable 2nd level cache, which I left but commented out for future reference.

However, I was not home and dry, because when the application started up, it could not find the slf4j api, which was not in any of the Hibernate jars. After googling around, I found on:

http://stackoverflow.com/questions/12926899/java-lang-noclassdeffounderror-org-slf4j-loggerfactory

that slf4j-api-1.7.2.jar and slf4j-jkd14-1.7.2jar needed to be downloaded from:

https://hub.jazz.net/project/morenoli/hangmangame/overview#https://hub.jazz.net/gerrit/plugins/gerritfs/list/morenoli%252Fhangmangame/refs%252Fheads%252Fmaster/WebContent/WEB-INF/lib

After downloading these two jar file and putting them in my Eclipse project, the application worked and generated only one SQL select statement, as stated in the tutorial.
 
H Paul
Ranch Hand
Posts: 471
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Good news that it worked.

2. Given you enjoy learning from youtube: consider watching and creating projects using Maven.
It will pull in dependent jar(s) for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic