2016-09-16 96 views
0

我相信所有的配置是正确的,但我仍正在错误:春天Declartive交易没有约束力

org.hibernate.HibernateException: save is not valid without active transaction 

一些如何声明性事务无法正常工作。你能指出什么可能是问题。

以下是我的配置:

<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:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <aop:aspectj-autoproxy /> 

    <context:property-placeholder location="classpath:application.properties" /> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
     <property name="driverClassName" value="${connection.driverclass}" /> 
     <property name="url" value="${connection.url}" /> 
     <property name="username" value="${connection.username}" /> 
     <property name="password" value="${connection.password}" /> 

    </bean> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocations"> 
      <list> 
       <value>classpath:hibernate.cfg.xml</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="baseDao" class="edu.hibernate.self.learning1.dao.impl.BaseDAO"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="employeeDAO" class="edu.hibernate.self.learning1.dao.impl.EmployeeDAOImpl" 
     parent="baseDao" /> 

    <bean id="h1TransactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="employeeService" 
     class="edu.hibernate.self.learning1.service.impl.EmployeeServiceImpl"> 
     <property name="employeeDAO" ref="employeeDAO" /> 
    </bean> 

    <tx:advice id="txAdviceEmpService" transaction-manager="h1TransactionManager"> 
     <tx:attributes> 
      <tx:method name="saveNewEmployee" propagation="REQUIRED" read-only="false" /> 
     </tx:attributes> 
    </tx:advice> 

    <aop:config> 
     <aop:advisor 
      pointcut="execution(* edu.hibernate.self.learning1.service.impl.EmployeeServiceImpl.*(..))" 
      advice-ref="txAdviceEmpService" /> 
    </aop:config> 

</beans> 

服务层:

public class EmployeeServiceImpl implements EmployeeService { 
    private EmployeeDAO employeeDAO; 

    @Override 
    public Employee saveNewEmployee(Employee employee) { 
     return employeeDAO.saveNewEmployee(employee); 
    } 

    public EmployeeDAO getEmployeeDAO() { 
     return employeeDAO; 
    } 

    public void setEmployeeDAO(EmployeeDAO employeeDAO) { 
     this.employeeDAO = employeeDAO; 
    } 
} 

回购层:

public class EmployeeDAOImpl extends BaseDAO implements EmployeeDAO { 
    @Override 
    public Employee saveNewEmployee(Employee employee) { 
     employee = (Employee) getCurrentSession().save(employee); 
     getCurrentSession().flush(); 
     return employee; 
    } 
} 

期待着一些回应。

回答

0

我认为这是因为你的切入点指向EmployeeService的实现。尝试将其更改为接口

+0

没有。没有成功。 – Debopam