2014-12-27 88 views
1

此前,我有BaseDAO與getTransaction()和commitTransaction()方法,它爲事務提供服務。但是,當我加@OneToMany懶加載的關係,我已經沒有會話等相關的錯誤,所以我決定用@Transactional註解我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註解新手,我不知道解決問題,這可能是重要的,我對混亂表示歉意):

<?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-調度-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> 

感謝。

找到解決方案,請參閱下面的答案。

回答