xml 代码
java 代码
xml 代码
我的配置文件和Dao如下,问题是save方法只能提交到缓存,并没有提交到数据库,找了很多资料都不能解决,只能放弃这种方法,挺不甘心的,网上很多人说是这样做,可是还是不行,希望能找到症结:
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" singleton="true">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.cglib.use_reflection_optimizer">trueprop>
<prop key="hibernate.proxool.xml">Proxool.xmlprop>
<prop key="hibernate.proxool.pool_alias">DBPoolprop>
props>
property>
<property name="mappingResources">
<list>
<value>hibernatePojo/Worker.hbm.xmlvalue>
list>
property>
bean>
<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="SessionFactory"/>
property>
bean>
<bean id="WorkerDAO" class="hibernateDaoImpl.WorkerDAOImpl">
<property name="sessionFactory">
<ref bean="SessionFactory"/>
property>
bean>
<bean id="WorkerDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="TransactionManager"/>
property>
<property name="proxyInterfaces">
<list>
<value>hibernateDaoInterface.WorkerDAOvalue>
list>
property>
<property name="target">
<ref bean="WorkerDAO"/>
property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIREDprop>
props>
property>
bean>
业务层
<bean id="WorkerLoginCheck" class="serviceImpl.WorkerLoginCheckImpl">
<property name="workerDAO">
<ref bean="WorkerDAOProxy"/>
property>
bean>
java 代码
DAO代码:
public class DAOImpl extends HibernateDaoSupport implements DAO {
public boolean insertOne(Object entity) {
try {
getHibernateTemplate().save(entity);
return true;
} catch (DataAccessException e) {
e.printStackTrace();
return false;
}
}
xml 代码
xml 代码
问题解决,问题在于事务的配置不对,事务应该配置在业务层而不是持久层,配在持久层的方法上就没有意义了
持久层配置文件:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="SessionFactory" />
property>
bean>
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref bean="transactionManager" />
property>
<property name="transactionAttributes">
<props>
<prop key="cha*">PROPAGATION_REQUIREDprop>
<prop key="update*">PROPAGATION_REQUIREDprop>
<prop key="del*">PROPAGATION_REQUIREDprop>
<prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>
props>
property>
bean>
<bean id="WorkerDAO" class="hibernateDaoImpl.WorkerDAOImpl">
<property name="sessionFactory">
<ref bean="SessionFactory" />
property>
bean>
业务层配置:
<bean id="WorkerLoginCheck" parent="baseTransactionProxy">
<property name="target">
<ref local="WorkerLoginCheckTarget" />
property>
bean>
<bean id="WorkerLoginCheckTarget" class="serviceImpl.WorkerLoginCheckImpl">
<property name="workerDAO">
<ref bean="WorkerDAO" />
property>
bean>
实现方法:
serviceImpl.WorkerLoginCheckImpl中有chaWorker方法,调用WorkerDAO的insertOne方法
很久了,一直没时间尝试,拖到了今天,希望能对大家有所帮助