I am learning how to configure Hibernate with EHCache but facing some issues with it.
I have below details in my `hibernate.cfg.xml file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hib</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
<mapping class="cache.three.Department"></mapping>
</session-factory>
</hibernate-configuration>
Below is my ehcache.xml
file:
<cache
name="com.examples.domain.Country"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
I am using Hibernate-4.3.8
version. Now when I run my program just to create a SessionFactory using this configuration I am getting below exception:
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
Exception in thread "main" java.lang.ExceptionInInitializerError
at example.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
at example.HibernateUtil.<clinit>(HibernateUtil.java:10)
at example.MyApp.storeData(MyApp.java:51)
at example.MyApp.main(MyApp.java:9)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:261)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:273)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at example.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
... 3 more
Caused by: org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from file:/home/Hibernate/bin/ehcache.xml. Initial cause was Error configuring from input stream. Initial cause was null:8: Element <cache> does not allow attribute "maxElementsInMemory".
at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:107)
at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:70)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251)
... 9 more
Please tell me know where I am making mistake in this configuration?
Update:
Replacing maxElementsInMemory
with maxEntriesLocalHeap
in ehcache.xml file is throwing below error:
Caused by: org.xml.sax.SAXException: null:8: Element <cache> does not allow attribute "maxEntriesLocalHeap".
The ehcache.xml file should be like this:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="200" />
<cache
name="com.examples.domain.Country"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>
It should start with ehcache
tag and also you need to declare a defaultCache
in it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.