使用ehcache的所有Hibernate实体类获取java.lang.ClassCastException

时间:2016-11-18 00:31:37

标签: java hibernate jpa ehcache

我使用的是版本“1.7.0_79”。 获得以下例外

2016-11-17 16:42:54,127 : ERROR: net.sf.ehcache.store.disk.DiskStorageFactory - Disk Write of
     

data_member_keys失败:       java.lang.ClassCastException:sun.reflect.GeneratedSerializationConstructorAccessor1无法强制转换   to sun.reflect.SerializationConstructorAccessorImpl         at sun.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:113)         at enter code here sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:331)         at java.io.ObjectStreamClass.getSerializableConstructor(ObjectStreamClass.java:1376)         at java.io.ObjectStreamClass.access $ 1500(ObjectStreamClass.java:72)         at java.io.ObjectStreamClass $ 2.run(ObjectStreamClass.java:493)         at java.io.ObjectStreamClass $ 2.run(ObjectStreamClass.java:468)         at java.security.AccessController.doPrivileged(Native Method)         在java.io.ObjectStreamClass。(ObjectStreamClass.java:468)         在java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:365)         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1133)         at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)         at net.sf.ehcache.util.MemoryEfficientByteArrayOutputStream.serialize(MemoryEfficientByteArrayOutputStream.java:97)         在net.sf.ehcache.store.disk.DiskStorageFactory.serializeElement(DiskStorageFactory.java:405)         at net.sf.ehcache.store.disk.DiskStorageFactory.write(DiskStorageFactory.java:384)         at net.sf.ehcache.store.disk.DiskStorageFactory $ DiskWriteTask.call(DiskStorageFactory.java:485)         at net.sf.ehcache.store.disk.DiskStorageFactory $ PersistentDiskWriteTask.call(DiskStorageFactory.java:1088)         at net.sf.ehcache.store.disk.DiskStorageFactory $ PersistentDiskWriteTask.call(DiskStorageFactory.java:1072)         在java.util.concurrent.FutureTask.run(FutureTask.java:262)         at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:178)         at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)         在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)         在java.lang.Thread.run(Thread.java:745)

jpa.xml

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.abc.xyz" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.connection.isolation">2</prop>
                <!-- should be turned off in production -->
                <prop key="hibernate.generate_statistics">false</prop>

                <!-- This is the provider for Ehcache provided by Hibernate, using the "old" SPI -->
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
                <prop key="hibernate.cache.use_second_level_cache">true"</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>


            </props>
        </property>
    </bean>

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?>

<ehcache>
    <diskStore path="java.io.tmpdir"/>

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
           maxElementsInMemory="50000"
           eternal="true"
           overflowToDisk="true"/>

    <cache name="org.hibernate.cache.internal.StandardQueryCache"
           maxElementsInMemory="50000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="true"
           diskPersistent="false"
               diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU"
            />

    <defaultCache
            maxElementsInMemory="50000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />

</ehcache>

有没有人知道任何解决方案。

1 个答案:

答案 0 :(得分:0)

您有类加载器问题。

发生此错误的唯一方法是,当前序列化的类是来自没有看到当前序列化程序使用的SerializationConstructorAccessorImpl的类加载器。

如果不知道您遇到此错误的环境,我无法再说清楚了。

我的猜测是你使用的是JDK 1.6。