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.