初学框架,遇到了很多麻烦,经过几天周折,终于小荷才露尖尖脚了,为了帮助像我一样的人,总结一下吧。现在一般书上的都是整合spring2.5+hibernate3的多,几乎没有新的。当时下载jar包时下的是hibernate4和spring3.2的,所以整合时,还以为会向下兼容,但整合时抛出的异常是一条又一条。
好吧,Linux打字是硬伤,重点说吧,当用spring集合Hibernate时,可以不用配置hibernate.cfg.xml了,但要配Entiry.hbm.xml然后加入到spring配置文件里面。
hibernate4的提升是自己有了自己的事物管理了,可以不用你去session.beginTransaction()和session.getTransaction().commit()了,只要你在Spring配置文件里面配置好事物管理通知:
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice1" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="set*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice1" pointcut="execution(* memorandum.service.*.*(..))" />
</aop:config>
但也要修改hibernate.current_session_context_class的值(H3是thread),修改为:org.springframework.orm.hibernate4.SpringSessionContext。
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 配置数据源 Hibernate支持 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root" />
<property name="password" value="yty534756893" />
<property name="url" value="jdbc:mysql://localhost:3306/one" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test/userm.hbm.xml</value>
<value>memorandum/dao/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--tx:annotation-driven transaction-manager="transactionManager"/-->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="execution(* test.*.*(..))" />
</aop:config>
<!--导入其他Bean配置文件-->
<import resource="test/userModel.xml"/>
<import resource="memorandum/action/actionForSpring.xml"/>
<!-- Bean Configuration -->
<bean id="productAction" class="product.action.ProductAction" scope="prototype" />
</beans>