我在spring4整合hibernate5遇到的問題
1,nested exception is java.lang.NoClassDefFoundError:org/hibernate/engine/SessionFactoryImplementor
hibernate4整合spring3 時出現 nested exception is java.lang.NoClassDefFoundError:org/hibernate/engine/SessionFactoryImplementor
異常
原因:3中這樣配置
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
<property name="nestedTransactionAllowed" value="true" />
</bean>
原因:4中該這樣配置
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name ="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
5跟4一樣設定
2, 解決Hibernate Write operations are not allowed in read-only mode的方法
錯誤資訊:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
解決方法:
在出現異常的方法中加入
getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER);
//hibernate3
getHibernateTemplate().setCheckWriteOperations(false);
//hibernate5
3,java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor
出現了Java.lang.NoClassDefFoundError:的錯誤,首先想到的就是少包,在網上搜搜了,果然是少了一個叫aopalliance.jar的jar包,下載這個包,加到路徑裡就OK了。
4,The prefix “tx” for element “tx:advice” is not bound 錯誤的說明
這個錯誤的原因是:我們在定義申明AOP的時候,沒有載入schema。
Spring的配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
新增之後Eclipse就能夠識別<tx:advice/>,<aop:config/>
標籤了。
5,Cannot locate the chosen ObjectFactory implementation
解決辦法是:新增struts2-spring-plugin-*.jar 包
6,java.lang.NoClassDefFoundError:
org/hibernate/engine/SessionFactoryImplementor
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="dataSource" ref="dataSource" /> <!-- 新增這一行 --!>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>