gpt4 book ai didi

java - 由于 'org.hibernate.HibernateException: No Session found for current thread' 异常,需要 @Transactional 注释

转载 作者:行者123 更新时间:2023-11-29 05:57:13 24 4
gpt4 key购买 nike

因此,我了解到放置 @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>

最佳答案

天哪,我能傻到什么程度呢?

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

这解决了所有问题:

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

关于java - 由于 'org.hibernate.HibernateException: No Session found for current thread' 异常,需要 @Transactional 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com