2

I have a little problem with hibernate.

I have some simple 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;    
}
}

here bean configuration for sessionFactory

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

If i remove @Transactional anotation from public User getUser(String login) i've got

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)

where i'm wrog ?

Ruslan F.
  • 5,498
  • 3
  • 23
  • 42

0 Answers0