Spring - @Transactional不会启动事务

时间:2014-12-27 19:17:23

标签: java spring hibernate spring-mvc transactions

早些时候,我使用了带有事务的getTransaction()和commitTransaction()方法的BaseDAO。但是当我在延迟加载时添加@OneToMany关系时,我遇到了与没有会话等相关的错误。所以我决定在我的 EmployerService 方法上使用@Transactional注释:

package services;

import daos.interfaces.InterfaceEmployerDAO;
import dtos.EmployerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import services.interfaces.InterfaceEmployerService;
import transformers.interfaces.InterfaceEmployerTransformer;

import java.util.List;

public class EmployerService implements InterfaceEmployerService {
    @Autowired
    private InterfaceEmployerDAO employerDAO;
    @Autowired
    private InterfaceEmployerTransformer employerTransformer;

    @Override
    @Transactional
    public List<EmployerDTO> getAllEmployers() {
        return employerTransformer.listToDTO(employerDAO.getAllEmployers());
    }

    (methods irrevelant at this moment)
}

并且<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />已添加到 applicationContext.xml 文件中(我粘贴所有文件,因为我是Spring Annotations中的新手,我不知道解决问题的重要性,我为混乱而道歉:

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

       <!-- Data Source Declaration -->
       <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
              <property name="driverClass" value="org.postgresql.Driver" />
              <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
              <property name="user" value="postgres" />
              <property name="password" value="postgres" />
              <property name="maxPoolSize" value="10" />
              <property name="maxStatements" value="0" />
              <property name="minPoolSize" value="5" />
       </bean>

       <!-- Session Factory Declaration -->
       <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="DataSource" />
              <property name="annotatedClasses">
                     <list>
                            <value>models.Employee</value>
                            <value>models.Employer</value>
                     </list>
              </property>
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                            <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
                            <prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</prop>
                            <prop key="hibernate.connection.username">postgres</prop>
                            <prop key="hibernate.connection.password">postgres</prop>
                            <prop key="hibernate.show_sql">true</prop>
                            <prop key="hibernate.current_session_context_class">thread</prop>
                            <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
                            <prop key="hibernate.hbm2ddl.auto">update</prop>
                            <prop key="hibernate.search.default.directory_provider">filesystem</prop>
                            <prop key="hibernate.search.default.indexBase">target/luceneIndex</prop>
                     </props>
              </property>
       </bean>

       <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="SessionFactory"></property>
       </bean>
       <bean class="services.EmployeeService"></bean>
       <bean class="services.EmployerService"></bean>
       <bean class="daos.EmployeeDAO"></bean>
       <bean class="daos.EmployerDAO"></bean>
       <bean class="transformers.EmployeeTransformer"></bean>
       <bean class="transformers.EmployerTransformer"></bean>

       <context:annotation-config/>
       <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
       <context:component-scan base-package="controllers" />

       <mvc:annotation-driven/>

</beans>

我认为HibernateTransactionManagertx:annotation-driven使其有效,但我错了 - 我在**createCriteria is not valid without active transaction**方法中得到了daos.EmployerDAO.getAllEmployers。我很确定我无法正确配置transactionManager。 如果有人决定帮助我,我会很高兴 - 提前谢谢你。 我还提出 mvc-dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix">
                     <value>/</value>
              </property>
              <property name="suffix">
                     <value>.jsp</value>
              </property>
       </bean>

</beans>

再次感谢您的帮助。

找到解决方案,请参阅下面的回答。

1 个答案:

答案 0 :(得分:0)

我认为我在这里找到了一个解决方案"spring, hibernate and declarative transaction implementation: there is no active transaction"。我必须删除这一行:

<prop key="hibernate.current_session_context_class">thread</prop>

因为它禁用了Spring事务管理。