Spring声明事务没有绑定

时间:2016-09-16 14:11:34

标签: spring hibernate spring-aop spring-transactions

我确信所有配置都是正确的,但我仍然收到错误:

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;
    }
}

期待一些回应。

1 个答案:

答案 0 :(得分:0)

我认为这是因为您的切入点指向EmployeeService的实现。尝试将其更改为界面