生活随笔
收集整理的這篇文章主要介紹了
在项目中集成hibernate遇到的问题(4)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在項目中集成hibernate時,運行Project后提示:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
在applicationContext.xml配置如下:
[html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?>? <beans? ??? xmlns="http://www.springframework.org/schema/beans"? ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">? ? ??? <import resource="applicatonDBContext.xml" />? ????? ????? ??? <bean id="userDao" class="com.hnyd.demo.hibernate.jdbc.dao.impl.UserDao">?????? ??? </bean>? </beans>?
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><import resource="applicatonDBContext.xml" /><bean id="userDao" class="com.hnyd.demo.hibernate.jdbc.dao.impl.UserDao"> </bean>
</beans>
applicatonDBContext.xml配置如下:
[html] view plaincopyprint?
<?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:jee="http://www.springframework.org/schema/jee"? ??? xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws"? ??? xmlns:context="http://www.springframework.org/schema/context"? ??? 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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"? ??? default-lazy-init="false">? ????? ??? <bean id="SystemDataSource"? class="com.mchange.v2.c3p0.ComboPooledDataSource"? destroy-method="close">???? ??????? <property name="driverClass"???????????????? value="${db.driver}" />? ??????? <property name="jdbcUrl"???????????????????? value="${db.url}" />? ??????? <property name="user"??????????????????????? value="${db.username}" />? ??????? <property name="password"??????????????????? value="${db.password}" />? ??????? <property name="minPoolSize"???????????????? value="10" />? ??????? <property name="maxPoolSize"???????????????? value="20" />? ??????? <property name="initialPoolSize"???????????? value="2" />? ??????? <property name="maxIdleTime"???????????????? value="1800" />? ??????? <property name="acquireIncrement"??????????? value="1" />? ??????? <property name="maxStatements"?????????????? value="0" />? ??????? <property name="idleConnectionTestPeriod"??? value="60" />? ??????? <property name="acquireRetryAttempts"??????? value="30" />? ??????? <property name="breakAfterAcquireFailure"??? value="false" />? ??????? <property name="testConnectionOnCheckout"??? value="false" />? ??????? <property name="testConnectionOnCheckin"???? value="true" />? ??? </bean>? ? ? ??? <bean id='ConfigurationBean'???????????? class="com.hnyd.demo.hibernate.util.Configuration" >? ??????? <property name="location"??????????? value="file:config/config.properties"/>? ??? </bean>? ????? ??? ? ??? <bean id="sessionFactory"??????????????? class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">? ??????? <property name="dataSource"????? ref="SystemDataSource"></property>? ??????? <property name="hibernateProperties">? ??????????? <props>? ??????????????? <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>? ??????????????? <prop key="hibernate.show_sql">true</prop>? ??????????????? ? ??????????????? <prop key="hibernate.jdbc.batch_size">20</prop>? ??????????? </props>? ??????? </property>? ??????? <property name="mappingResources">? ??????????? <list>? ??????????????? <value>hbm/User.hbm.xml</value>? ??????????? </list>? ??????? </property>? ??? </bean>? ????? ??? ? ??? <bean id="hibernateTemplate"???????????? class="org.springframework.orm.hibernate3.HibernateTemplate">? ??????? <property name="sessionFactory">? ??????????? <ref bean="sessionFactory" />? ??????? </property>? ??? </bean>? ????? ??? <bean id="jdbcTemplate"????????? class="org.springframework.jdbc.core.JdbcTemplate">? ??????? <property name="dataSource"????? ref="SystemDataSource" />? ??? </bean>? ? ? ??? <bean id="sjdbcTemplate"???????????? class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">? ??????? <constructor-arg>? ??????????? <ref bean="SystemDataSource" />? ??????? </constructor-arg>? ??? </bean>? </beans>?
<?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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:context="http://www.springframework.org/schema/context"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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"default-lazy-init="false"><bean id="SystemDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${db.driver}" /><property name="jdbcUrl" value="${db.url}" /><property name="user" value="${db.username}" /><property name="password" value="${db.password}" /><property name="minPoolSize" value="10" /><property name="maxPoolSize" value="20" /><property name="initialPoolSize" value="2" /><property name="maxIdleTime" value="1800" /><property name="acquireIncrement" value="1" /><property name="maxStatements" value="0" /><property name="idleConnectionTestPeriod" value="60" /><property name="acquireRetryAttempts" value="30" /><property name="breakAfterAcquireFailure" value="false" /><property name="testConnectionOnCheckout" value="false" /><property name="testConnectionOnCheckin" value="true" /></bean><bean id='ConfigurationBean' class="com.hnyd.demo.hibernate.util.Configuration" ><property name="location" value="file:config/config.properties"/></bean><!-- 創建一個sessionFactory工廠,用于注入hibernate --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="SystemDataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --><prop key="hibernate.jdbc.batch_size">20</prop></props></property><property name="mappingResources"><list><value>hbm/User.hbm.xml</value></list></property></bean><!-- 創建一個hibernate模板 --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="SystemDataSource" /></bean><bean id="sjdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"><constructor-arg><ref bean="SystemDataSource" /></constructor-arg></bean>
</beans>
UserDao.java
[java] view plaincopyprint?
public class UserDao extends HibernateDaoSupport implements IUserDao {? ? ??? @Override? ??? public void add(User user) {? ??????? this.getHibernateTemplate().save(user);? ??? }? ? ??? @Override? ??? public void delete(int uid) {? ??????? User user=new User();? ??????? user.setId(uid);? ??????? this.getHibernateTemplate().delete(user);? ??? }? ? ??? @Override? ??? public User findById(int uid) {? ??????? return (User)this.getHibernateTemplate().get(User.class,uid);? ??? }? }?
public class UserDao extends HibernateDaoSupport implements IUserDao {@Overridepublic void add(User user) {this.getHibernateTemplate().save(user);}@Overridepublic void delete(int uid) {User user=new User();user.setId(uid);this.getHibernateTemplate().delete(user);}@Overridepublic User findById(int uid) {return (User)this.getHibernateTemplate().get(User.class,uid);}
}
查找 原因發現HibernateDaoSupport缺少注入SessionFactory,HibernateDaoSupport提供了基于AOP事務的自動處理,程序員完全可以不用理會事務的開始與提交。在JDBC中一個Connection對象使用一個事務,那么在Hibernate中一個事務肯定要關聯一個SessionFactory了,然而這個SessionFactory卻沒有在DAO中體現。其實主要的原因是HibernateDaoSupport類已經默默地做了封裝的工作,它用一個setSessionFactory方法將SessionFactory進行注入,所以繼承自HibernateDaoSupport類的DAO都會具有SessionFactory的屬性,從而可以通過SessionFactory創建Session實例操作數據庫。
在繼承HibrnateDaoSupport的DAO實現里,Hibernate Session的管理完全不需要Hibernate代碼打開,而由Spring來管理。Spring會根據實際的操作,采用“每次事務打開一次session”的策略,自動提高數據庫訪問的性能。
因此需要定義一個父類,父類要有一個hibernateTemplate屬性,并且注入引用,配置如下:
[html] view plaincopyprint?
? <bean id="daoTemplate" abstract="true">? <property name="hibernateTemplate">? ??????? <ref bean="hibernateTemplate" />? ??? </property>? </bean>?
<!-- 創建一個dao注入的模板 -->
<bean id="daoTemplate" abstract="true">
<property name="hibernateTemplate"><ref bean="hibernateTemplate" /></property>
</bean>
所有使用hibernate的dao都配置為該類的子類,于是applicationContext.xml配置如下:
[html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?>? <beans? ??? xmlns="http://www.springframework.org/schema/beans"? ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">? ? ??? <import resource="applicatonDBContext.xml" />? ????? ??? ? ??? <bean id="daoTemplate" abstract="true">? ??????? <property name="hibernateTemplate">? ??????????? <ref bean="hibernateTemplate" />? ??????? </property>? ??? </bean>? ????? ??? <bean id="userDao" class="com.hnyd.demo.hibernate.jdbc.dao.impl.UserDao" parent="daoTemplate">????? ??? </bean>? </beans>?
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><import resource="applicatonDBContext.xml" /><!-- 創建一個dao注入的模板 --><bean id="daoTemplate" abstract="true"><property name="hibernateTemplate"><ref bean="hibernateTemplate" /></property></bean><bean id="userDao" class="com.hnyd.demo.hibernate.jdbc.dao.impl.UserDao" parent="daoTemplate"> </bean>
</beans>
經過上面的配置后,就可以正常使用了
總結
以上是生活随笔為你收集整理的在项目中集成hibernate遇到的问题(4)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。