最近自己搭建了一个ssh的框架,在spring 整合 hibernate这块不是很理解,希望大家帮助我解决问题。
这个是我的spring配置文件,对于hibernate事务,我选择AOP的形式去管理事务。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
classpath:/org/springframework/transaction/config/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
classpath:/org/springframework/aop/config/spring-aop-3.2.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.*"/>
<!-- 引用jdbc配置文件 -->
<context:property-placeholder location="WEB-INF/jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="acquireIncrement" value="1"></property>
<property name="initialPoolSize" value="5"></property>
<property name="maxIdleTime" value="60"></property>
<property name="maxPoolSize" value="5"></property>
<property name="minPoolSize" value="1"></property>
<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<!-- 配置hibernate sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:mappingLocations="classpath:/com/model/**/*.hbm.xml">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
<!-- 如果没有此配置getCurrentSession()时就会报错:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here -->
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate3.SpringSessionContext
</prop>
</props>
</property>
</bean>
<!-- Transaction Manager 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 把切面注入到事务中 -->
<aop:config>
<aop:pointcut id="productServiceMethods" expression="execution(* com.ssh.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
</aop:config>
</beans>
这是我的dao层基类
public class BaseDao {
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
//return sessionFactory.openSession();
}
protected void delete(Object object) {
this.getSession().delete(object);
}
@SuppressWarnings("unchecked")
protected <T> T get(Class<T> entityClass, long id) {