org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/config/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Could not instantiate cache implementation
……
……
……
先把這段異常資訊google一把後,得到大部分的解決方案都說是包衝突,在經過一系列的實驗發現根本就不是一回事,繼續尋找下列異常資訊。
Caused by: org.hibernate.HibernateException: Could not instantiate cache implementation
……
……
……
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
隱約得知緩衝配置方面出現問題,發現問題後順著指示找到了applicationContext.xml檔案中的這段配置,提示的意思大概為緩衝不可用,在添加了緩衝配置後一切正常。
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 添加緩衝配置-->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
<!-- hbm.xml檔案載入方式-->
<property name="mappingLocations">
<list>
<value>classpath*:/com/train/entity/admin/hbm/*.hbm.xml</value>
<value>classpath*:/com/train/entity/common/hbm/*.hbm.xml</value>
</list>
</property>
</bean>
雖然報的異常與網上其他人幾乎相同,但還是有不一致的地方,對症下藥方可藥到病除。