我的应用程序是spring,struts2和hibernate,数据库是postgress 9.2。问题是DAO方法没有使用flush来提交数据。

我的spring.xml:

<?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:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee     http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">




<context:component-scan base-package="dao" />

<jee:jndi-lookup id="depo" jndi-name="java:/depo"/>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="depo"/>


    <property name="hibernateProperties">
        <props>
            <!--    <prop     key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.format_sql">true</prop>
            <!--    <prop key="hibernate.hbm2ddl.auto">update</prop> -->
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>

            <value>model.AtOrganisation</value>

        </list>
    </property>

</bean>
<bean id="transactionManager"   class="org.springframework.transaction.jta.JtaTransactionManager" />

<tx:annotation-driven transaction-manager="transactionManager" />


<bean id="orgdao" class="dao.OrganisationDaoImp">
    <property name="sessionfactory" ref="sessionFactory" />
</bean>

<bean id="empAction" class="action.OraganisationAction">
    <property name="orgdao" ref="orgdao" />
</bean>




我的DAO课是:

 @Override
@Transactional(propagation = Propagation.REQUIRED)
public void addOrg(AtOrganisation org) {

    Session session = sessionfactory.openSession();

    session.saveOrUpdate(org);
    //session.flush();


}


任何人都可以指出我在这里错过了什么。
注意:如果取消注释,则flush()数据将提交。

最佳答案

您实际上需要通过调用以下内容来检索Spring正在使用的同一会话:

Session session = sessionFactory.getCurrentSession();


openSession()的调用相反,对getCurrentSession()的调用将创建一个不受Spring管理的全新会话,而对@Transactional的调用则检索由机制管理的同一会话。

10-04 23:24
查看更多