title: org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped [from Team]
body: <p>I'm working on little Spring MVC CRUD application. Got some strange problems:</p>
  
  <ul>
  <li><p>configuration class:</p>
  
  <pre><code>package sbk.spring.simplejc.config;
  
  import java.util.Properties;
  
  import javax.annotation.Resource;
  import javax.sql.DataSource;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate4.HibernateTransactionManager;
  import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  import org.springframework.web.servlet.view.JstlView;
  import org.springframework.web.servlet.view.UrlBasedViewResolver;
  
  @Configuration //Specifies the class as configuration
  @ComponentScan("sbk.spring.simplejc") //Specifies which package to scan
  //@Import({DataBaseConfig.class})
  @EnableTransactionManagement
  @PropertySource("classpath:application.properties")
  @EnableWebMvc //Enables to use Spring's annotations in the code
  public class WebAppConfig extends WebMvcConfigurerAdapter{
      private static final String PROPERTY_NAME_DATABASE_DRIVER                   = "db.driver";
      private static final String PROPERTY_NAME_DATABASE_PASSWORD                 = "db.password";
      private static final String PROPERTY_NAME_DATABASE_URL                      = "db.url";
      private static final String PROPERTY_NAME_DATABASE_USERNAME                 = "db.username";
  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT                 = "hibernate.dialect";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL                = "hibernate.show_sql";
      private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN    = "entitymanager.packages.to.scan";
      @Resource
      private Environment env;
  
      @Bean
      public DataSource dataSource() {
              DriverManagerDataSource dataSource = new DriverManagerDataSource();
              dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
              dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
              dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
              dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
              return dataSource;
      }
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
          sessionFactoryBean.setDataSource(dataSource());     
          sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
          sessionFactoryBean.setHibernateProperties(hibProperties()); 
          return sessionFactoryBean;
      }   
      private Properties hibProperties() {
              Properties properties = new Properties();
              properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
              properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
              return properties;        
      }
      @Bean
      public HibernateTransactionManager transactionManager() {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(sessionFactory().getObject());
          return transactionManager;
      }
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
              registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
      }
      @Bean
      public UrlBasedViewResolver setupViewResolver() {       
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/views/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  }
  </code></pre></li>
  <li><p><code>application.properties</code>:</p>
  
  <pre><code>#DB properties:  
  db.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver 
  db.url=jdbc:sqlserver://127.0.0.1:1433;databaseName=Examples 
  db.username=sa
  db.password=
  
  #Hibernate Configuration:  
  hibernate.dialect=org.hibernate.dialect.SQLServerDialect
  hibernate.show_sql=true  
  entitymanager.packages.to.scan=sbk.spring.simplejc.entity 
  </code></pre></li>
  <li><p><code>@Entity</code> class:</p>
  
  <pre><code>package sbk.spring.simplejc.entity;
  
  
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;
  
      @Entity
      @Table(name="Team")
      public class Team {
          @Id
          @GeneratedValue
          private Integer id;
          private String name;
          private Integer rating;
          public Integer getId() {
              return id;
          }
          public void setId(Integer id) {
              this.id = id;
          }
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public Integer getRating() {
              return rating;
          }
          public void setRating(Integer rating) {
              this.rating = rating;
          }
      }
  </code></pre></li>
  <li><p>Controller class:</p>
  
  <pre><code>package sbk.spring.simplejc.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.servlet.ModelAndView;
  import sbk.spring.simplejc.service.ITeamService;
  
  
  @Controller
  public class TeamController {
      @Autowired
      ITeamService service;
  
      @RequestMapping(value="/")
      public ModelAndView goToHelloPage() {
          ModelAndView view = new ModelAndView();
          view.addObject("teamList", service.listTeams());
          return view;
      }
  
  }
  </code></pre></li>
  <li><p>Error stack trace:</p>
  
  <pre><code>    org.hibernate.hql.internal.ast.QuerySyntaxException: Team is not mapped [from Team]
          org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
          org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
          org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
          org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
          org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
          org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
          org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
          org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
          org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
          org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
          org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
          org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
          org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
          org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:105)
          org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:80)
          org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
          org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
          org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
          org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1777)
          sbk.spring.simplejc.dao.HibTeamDAO.listTeams(HibTeamDAO.java:23)
          sbk.spring.simplejc.service.TeamService.listTeams(TeamService.java:27)
  </code></pre></li>
  </ul>
  
  <p>I haven't got a clue about this issue.</p>
  
  <hr>
  
  <h2>Update</h2>
  
  <ul>
  <li><p>DAO class:</p>
  
  <pre><code>package sbk.spring.simplejc.dao;
  
      import java.util.List;
  
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Repository;
      import sbk.spring.simplejc.entity.Team;
  
      @Repository
      public class HibTeamDAO implements TeamDAO {    
          @Autowired
          private SessionFactory sessionFactory;
          public void addTeam(Team team) {
              sessionFactory.getCurrentSession().save(team);
          }
          public void updateTeam(Team team) {     
              sessionFactory.getCurrentSession().update(team);
          }
          @SuppressWarnings("unchecked")
          public List&lt;Team&gt; listTeams() {
              return sessionFactory.getCurrentSession().createQuery("from Team").list();
          }
          @SuppressWarnings("unchecked")
          public Team getTeamById(Integer teamID) {
              Session     session         =       sessionFactory.getCurrentSession();     
              List&lt;Team&gt;  listTeam        =       session.createQuery("from Team t where t.id = :teamID")
                                                  .setParameter("teamID", teamID)
                                                  .list();
              return listTeam.size()  &gt; 0 ? (Team)listTeam.get(0) : null;
          } 
          public void removeTeam(Integer teamID) {
              Team team = (Team) sessionFactory.getCurrentSession().load(Team.class, teamID);
              if(team != null){
                  sessionFactory.getCurrentSession().delete(team);
              }
          }
          @Override
          public Integer count() {
              return (Integer) sessionFactory.getCurrentSession().createQuery("select count(t) from Team t").uniqueResult();
          }
      }
  </code></pre></li>
  <li><p><code>TeamController</code> class:</p>
  
  <pre><code>package sbk.spring.simplejc.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.servlet.ModelAndView;
  import sbk.spring.simplejc.service.ITeamService;
  
  
  @Controller
  public class TeamController {
      @Autowired
      ITeamService service;
  
      @RequestMapping(value="/")
      public ModelAndView goToHelloPage() {
          ModelAndView view = new ModelAndView();
          view.addObject("teamList", service.listTeams());
          return view;
      }
  
  }
  </code></pre></li>
  </ul>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>Now I got rid from this problem by changing DAO method from </p>
  
  <pre><code>return sessionFactory.getCurrentSession().createQuery("from Team").list();
  </code></pre>
  
  <p>to</p>
  
  <pre><code>return sessionFactory.getCurrentSession().createQuery("from sbk.spring.simplejc.entity.Team").list();
  </code></pre>
  
  <p>But received another issue: every query return null despite of existing rows in <code>Team</code> table.</p>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>Finally I noticed warning messages:</p>
  
  <pre><code>Feb 15, 2014 7:01:05 PM org.hibernate.hql.internal.QuerySplitter concreteQueries
  WARN: HHH000183: no persistent classes found for query class: from sbk.spring.simplejc.entity.Team
  </code></pre>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>At least I've sorted out this issue by adding next row of code in <code>dataSource</code> bean definition in <code>WebAppConfig</code>:</p>
  
  <pre><code>public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
      sessionFactoryBean.setDataSource(dataSource());     
      sessionFactoryBean.setAnnotatedClasses(new Class[]{Team.class});//new row!!!
      sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
      sessionFactoryBean.setHibernateProperties(hibProperties()); 
      return sessionFactoryBean;
  }   
  </code></pre>