Spring Hibernate SessionFactory配置

时间:2013-07-26 08:02:32

标签: java spring hibernate

我对hibernate有点问题。

我有一些简单的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 anotation

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)

我在哪里?

0 个答案:

没有答案