1

所以,我读到放置@Transactional 注释的最佳位置是在包含数据库访问方法的DAO 类之外,就像在使用这些方法的服务类中一样。

现在,问题是,一旦我已经从 DAO 类中删除了这些注释,我就会启动 DAO 测试方法并引发上述异常。我将注释放回 DAO 类中,并且不再引发此异常。

然后我的问题是:我怎样才能清除我的 DAO 中的这些注释并且仍然让我的测试正常工作?

让我们添加一些代码:

DAO 类

public class UserDAO  extends IDAO implements IUserDAO {


    @Override
    //@Transactional(readOnly=true)
    public User get(int idUser) {
        return (User) currentSession().get(User.class,idUser);
    }}

IDAO类

public abstract class IDAO {

    protected SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session currentSession()
    {
        return sessionFactory.getCurrentSession();
    }

}

测试班

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/app-context.xml" })
public class UserDAOTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private IUserDAO userDAO;

    @Test
    public void testGetUser() throws Exception {
        User user = userDAO.get(2);
        assertNotNull(user);
    }
}

应用程序配置

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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://localhost:3306/waldb" />
        <property name="username" value="user" />
        <property name="password" value="password" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.wal.serverside.persistence.domain.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

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

    <bean id="userDAO" class="com.wal.serverside.persistence.DAO.UserDAO">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

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

    <context:component-scan base-package="com.wal.serverside.persistence" />

</beans>
4

2 回答 2

2

天哪,我能有多傻?

我的测试类没有从AbstractTransactionalJUnit4SpringContextTests扩展,所以我的测试中既没有事务也没有会话。

这解决了这一切:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/app-context.xml" })
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
public class UserDAOTest extends AbstractTransactionalJUnit4SpringContextTests  {

    @Autowired
    private IUserDAO userDAO;

    public void setUserDAO(IUserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Test
    public void testGetUser() throws Exception {
        User user = userDAO.get(2);
        assertNotNull(user);
    }
}
于 2012-07-23T09:08:37.260 回答
1

尝试不仅在您的测试类上添加@Transactional注释,还要在@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)上添加。您在其中显式设置您在 xml.xml 中定义的事务管理器的名称。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/app-context.xml" })
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Exception.class})
public class UserDAOTest {
     ...
}

如果您在测试方法中显式创建应用程序上下文然后从中获取 bean,则事务也将不起作用:

ApplicationContext appContext = new ClassPathXmlApplicationContext(...);
SomeDAO someDAO = (SomeDAO) appContext.getBean(...);

而不是注入它。

但我看到这不是你的情况。

于 2012-07-23T08:50:30.073 回答