java - Hibernate EHCache 程序的问题

标签 java hibernate ehcache

我正在学习如何使用 EHCache 配置 Hibernate,但遇到了一些问题。

我的 `hibernate.cfg.xml 文件中有以下详细信息:

<?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>

下面是我的ehcache.xml文件:

<cache
    name="com.examples.domain.Country"
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600"
    overflowToDisk="true"
/>

我正在使用 Hibernate-4.3.8 版本。现在,当我运行我的程序只是为了使用此配置创建一个 SessionFactory 时,我遇到了以下异常:

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

请告诉我我在这个配置中哪里出错了?

更新:

在 ehcache.xml 文件中用 maxEntriesLocalHeap 替换 maxElementsInMemory 会抛出以下错误:

Caused by: org.xml.sax.SAXException: null:8: Element <cache> does not allow attribute "maxEntriesLocalHeap".

最佳答案

ehcache.xml 文件应该是这样的:

<?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>

它应该以ehcache 标签开头,并且您还需要在其中声明一个defaultCache

关于java - Hibernate EHCache 程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29848945/

相关文章:

java - 当有人点击保存按钮时显示一个弹出窗口(JDialog)

sql-server - Hibernate 中的 setMaxResults(N) 如何工作?

java - Hibernate 不会完全刷新实体子项

java - 具有 Hibernate 二级缓存的 Jhipster Multi-Tenancy

java - 从 play framework 2.0.8 迁移到 2.1.0 错误 : cache play already exists

Java Swing - 如何在任何 JOptionPane 之前发出蜂鸣声?

java - 在没有 JConsole 的情况下使用 JMX 执行方法

java - 计算事件的下一个日期

java - 如何自动创建 Multi-Tenancy 架构?

java - 在 Java EE 应用程序中缓存