This exception occurred when I was trying Session Factory Level Cache. I tried to find out the reason for it by visiting many sites. But I didn't get the reason for it. Later I got the solution for this problem.
The Java Project structure is shown in the below figure.Student class has the following properties shown in the same figure.
Hibernate 5.2.8 is used in this example project. All the required jars of hibernate framework are added to the build path. Oracle 11g Express Edition is used as DB.
The ehcache.xml contains the following code:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
updateCheck="false">
<defaultCache maxElementsInMemory="1000" eternal="false"
timeToLiveSeconds="60" overflowToDisk="false" />
</ehcache>
student.hbm.xml contains the following code:
oracle.cfg.xml contains the following code:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.Student" table="student" schema="system">
<cache usage="read-write" />
<id name="id"></id>
<property name="name"></property>
<property name="email"></property>
<property name="marks"></property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">kiru</property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<!-- use second level cache -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.region.factory_class">org.hibernate. cache.ehcache. EhCacheRegionFactory</property>
<property name="net.sf.ehcache.configurationResourceName">resources /ehcache.xml</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="resources/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
SessionLevelFactoryCache.java contains the following code:
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import beans.Student;
public class SessionFactoryLevelCache {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("resources/oracle.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Student st1 = (Student) s.get(Student.class, 13);
System.out.println(st1.getName());
Student st2 = (Student) s.get(Student.class, 13);
System.out.println(st2.getName());
Session s2 = sf.openSession();
Student st3 = (Student) s2.get(Student.class, 13);
System.out.println(st3.getName());
s.close();
s2.close();
sf.close();
}
}
The above error was coming when i tried to execute the above class. Then i tried to add the optional jars (ehcache jars) present in the optional folder in Hibernate 5.2.8-final.jar
The following jars are present in the ehcache folder.
For working with cache, we need to add these 3 jars to the build path of the project. After adding the 3 jars, the program executed without any errors as shown in the below fig.