2

我对休眠有一点问题。

我有一些简单的 DAO

@Repository
public class UserDAOImpl implements UserDAO {

@Autowired
private SessionFactory sessionFactory;

private Session openSession() {
    return sessionFactory.getCurrentSession();
}
@Transactional
public User getUser(String login) {
    List<User> userList;
    Query query = openSession().createQuery("from User u where u.login = :login");
    query.setParameter("login", login);
    userList = query.list();
    if (userList.size() > 0)
        return userList.get(0);
    else
        return null;    
}
}

这里是 sessionFactory 的 bean 配置

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

public User getUser(String login)如果我从我得到的 @Transactional 注释中删除

org.hibernate.HibernateException: No Session found for current thread
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)

我在哪里?

4

0 回答 0