title: Hibernate: createQuery is not valid without active transaction
body: <p>I have some problems with my Spring + Hibernate project. When i try to fetch data, i have:</p>
  
  <p>HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction
  Here my code for xml config:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/users" /&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value="12345"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="SessionFactory"
                 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;com.springapp.model.User&lt;/value&gt;
              &lt;value&gt;com.springapp.model.UserRole&lt;/value&gt;
              &lt;value&gt;com.springapp.model.Project&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="SessionFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>Method from DAO</p>
  
  <pre><code>@Override
  public List&lt;User&gt; showAllUsers(){
      List&lt;User&gt; users = new ArrayList&lt;User&gt;();
      Session session = this.sessionFactory.getCurrentSession();
      users = session.createQuery("from User").list();
      return users;
  }
  </code></pre>
  
  <p>All services marked as Transactionals. When I delete 
  <code>&lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;</code>
  I have Hibernate error: No Session found for current thread. How can I solve this problem?</p>
  
  <p><strong>UPDATED #2</strong>
  Full dao</p>
  
  <pre><code>@Autowired
  private SessionFactory sessionFactory;
  
  @SuppressWarnings("unchecked")
  public User findByUserName(String username) {
  
      List&lt;User&gt; users = new ArrayList&lt;User&gt;();
  
      Session session = this.sessionFactory.getCurrentSession();
      session.beginTransaction();
      users = session
              .createQuery("from User where username=?")
              .setParameter(0, username).list();
      session.getTransaction().commit();
      session.close();
      if (users.size() &gt; 0) {
          return users.get(0);
      } else {
          return null;
      }
  }
  
  @Override
  public void addUser(User user){
      Session session = this.sessionFactory.getCurrentSession();
      session.save(user);
  }
  
  @Override
  public void deleteUser(User user){
      Session session = this.sessionFactory.getCurrentSession();
      System.out.println(user.getUsername());
      session.delete(user);
  }
  @Override
  public void verifyUser(User user){
      Session session = this.sessionFactory.getCurrentSession();
      session.update(user);
  }
  @Override
  public List&lt;User&gt; showAllUsers(){
      List&lt;User&gt; users = new ArrayList&lt;User&gt;();
      Session session = this.sessionFactory.getCurrentSession();
      session.beginTransaction();
      users = session.createQuery("from User").list();
      session.getTransaction().commit();
      session.close();
      return users;
  }
  </code></pre>
  
  <p>Now I have "nested exception is org.hibernate.SessionException: Session was already closed"
  What's the matter?</p>
  
  <p><strong>UPDATE</strong>
  This is how I called this methods:</p>
  
  <pre><code>@RequestMapping("/admin/updateUser")
  public String updateUser(ModelMap model,@ModelAttribute("userId") String username)
  {
      User user = this.userService.findByUserName(username);
      if(user.isEnabled()) user.setEnabled(false);
      else user.setEnabled(true);
      this.userService.updateUser(user);
      return "redirect:/admin";
  }
   @RequestMapping("/admin")
  public String adminPanel(ModelMap model)
  {
      List&lt;User&gt; users = this.userService.showAllUsers();
      model.addAttribute("users",users);
      return "adminpanel";
  }
  </code></pre>