title: JPA/Hibernate mapping: “QuerySyntaxException: Player is not mapped…”
body: <p>I'm having a really basic problem setting up a new Hibernate project.</p>
  
  <p>I have an Entity as follows:</p>
  
  <pre><code>@Entity(name = "Player")
  public class PlayerEntity implements Serializable { 
  
      private static final long serialVersionUID = 6584040856373261900L;
  
      @Id
      private long id;
  
      @Column
      private String forename;
  
      @Column
      private String surname;
  
      @Column
      private String team;
  
      @Column
      @Enumerated(EnumType.STRING)
      private Position position;
  
      @Column
      private boolean selected;
  
      @Column
      private int totalPoints;
  
      public PlayerEntity() {
      }
  
      public PlayerEntity(final long id, final String forename, final String surname, final String team,
              final Position position, final boolean selected, final int totalPoints) {
          this.id = id;
          this.forename = forename;
          this.surname = surname;
          this.team = team;
          this.position = position;
          this.selected = selected;
          this.totalPoints = totalPoints;
      }
  
      /**
       * @return the id
       */
      public long getId() {
          return id;
      }
  
      /**
       * @param id the id to set
       */
      public void setId(long id) {
          this.id = id;
      }
  
      /**
       * @return the forename
       */
      public String getForename() {
          return forename;
      }
  
      /**
       * @param forename the forename to set
       */
      public void setForename(String forename) {
          this.forename = forename;
      }
  
      /**
       * @return the surname
       */
      public String getSurname() {
          return surname;
      }
  
      /**
       * @param surname the surname to set
       */
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
      /**
       * @return the team
       */
      public String getTeam() {
          return team;
      }
  
      /**
       * @param team the team to set
       */
      public void setTeam(String team) {
          this.team = team;
      }
  
      /**
       * @return the position
       */
      public Position getPosition() {
          return position;
      }
  
      /**
       * @param position the position to set
       */
      public void setPosition(Position position) {
          this.position = position;
      }
  
      /**
       * @return the selected
       */
      public boolean isSelected() {
          return selected;
      }
  
      /**
       * @param selected the selected to set
       */
      public void setSelected(boolean selected) {
          this.selected = selected;
      }
  
      /**
       * @return the totalPoints
       */
      public int getTotalPoints() {
          return totalPoints;
      }
  
      /**
       * @param totalPoints the totalPoints to set
       */
      public void setTotalPoints(int totalPoints) {
          this.totalPoints = totalPoints;
      }
  </code></pre>
  
  <p>And a data service which accesses the data as follows:</p>
  
  <pre><code>public class PlayerDataServiceJpa implements PlayerDataService {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      public void addPlayers(final List&lt;Player&gt; players) {
          for (final Player domainPlayer : players) {
              final PlayerEntity entityPlayer = new PlayerEntity();
  
              BeanUtils.copyProperties(domainPlayer, entityPlayer);
  
              entityManager.persist(entityPlayer);
          }
      }
  
      public List&lt;Player&gt; getPlayers() {
          final List&lt;Player&gt; domainPlayers = new ArrayList&lt;Player&gt;();
  
          final List&lt;PlayerEntity&gt; entityPlayers = entityManager.createQuery("SELECT tc FROM PlayerEntity tc", PlayerEntity.class).getResultList();
  
          for (final PlayerEntity entityPlayer : entityPlayers) {
              final Player domainPlayer = new Player();
  
              BeanUtils.copyProperties(entityPlayer, domainPlayer);
  
              domainPlayers.add(domainPlayer);
          }
  
          return domainPlayers;
      }
  </code></pre>
  
  <p>I'm then trying to unit test the data service as follows:</p>
  
  <pre><code>@Transactional      
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(value = {"/hsqlDatasourceContext.xml", "/testApplicationContext.xml"})
  public class PlayerDataServiceJpaTest {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Autowired
      @Qualifier(value = "playerDataServiceJpa")
      private PlayerDataServiceJpa playerDataServiceJpa;
  
      @Test
      public void testGetPlayers() {
          // arrange
          final PlayerEntity player = TestDataUtil.createEntityPlayer(1);
          entityManager.persist(player);
  
          PlayerEntity find = entityManager.find(PlayerEntity.class, TestDataUtil.PLAYER_1_ID);
          assertThat(find).isNotNull();
          assertThat(find.getForename()).isEqualTo(TestDataUtil.PLAYER_1_FORENAME);
  
          // act
          final List&lt;Player&gt; players = playerDataServiceJpa.getPlayers();
  
          // assert
          assertThat(players).hasSize(1);
  
          final Player retrievedPlayer = players.get(0);
          assertThat(retrievedPlayer.getId()).isEqualTo(TestDataUtil.PLAYER_1_ID);
          assertThat(retrievedPlayer.getForename()).isEqualTo(TestDataUtil.PLAYER_1_FORENAME);
          assertThat(retrievedPlayer.getSurname()).isEqualTo(TestDataUtil.PLAYER_1_SURNAME);
          assertThat(retrievedPlayer.getTeam()).isEqualTo(TestDataUtil.TEST_TEAM);
          assertThat(retrievedPlayer.getTotalPoints()).isEqualTo(TestDataUtil.PLAYER_1_POINTS);
      }
  
      @Test
      public void testAddPlayers() {
          // arrange
          final Player player1 = TestDataUtil.createModelPlayer(1);
          final Player player2 = TestDataUtil.createModelPlayer(2);
  
          // act
          playerDataServiceJpa.addPlayers(Arrays.asList(player1, player2));
  
          // assert
          final List&lt;Player&gt; players = playerDataServiceJpa.getPlayers();
  
          assertThat(players).hasSize(2);
  
          final Player retrievedPlayer = players.get(0);
          assertThat(retrievedPlayer.getId()).isEqualTo(TestDataUtil.PLAYER_1_ID);
          assertThat(retrievedPlayer.getForename()).isEqualTo(TestDataUtil.PLAYER_1_FORENAME);
          assertThat(retrievedPlayer.getSurname()).isEqualTo(TestDataUtil.PLAYER_1_SURNAME);
          assertThat(retrievedPlayer.getTeam()).isEqualTo(TestDataUtil.TEST_TEAM);
          assertThat(retrievedPlayer.getTotalPoints()).isEqualTo(TestDataUtil.PLAYER_1_POINTS);
      }
  </code></pre>
  
  <p>However, when I run the unit test, the persist part of the code seems to work but when the query runs I get the following:</p>
  
  <pre><code>org.hibernate.hql.internal.ast.QuerySyntaxException: PlayerEntity is not mapped
      at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
      at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
  </code></pre>
  
  <p>My persistence.xml file which is living in src/test/resources/META-INF is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence       http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;
      &lt;persistence-unit name="jpaData" transaction-type="RESOURCE_LOCAL"&gt;
  
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; 
          &lt;class&gt;aaa.bbb.ccc.entity.PlayerEntity&lt;/class&gt;
  
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And the hsqlDatasourceContext.xml file looks like</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd   http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd    http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
      xmlns:jdbc="http://www.springframework.org/schema/jdbc"&gt;
  
      &lt;!-- HSQLDB datasource --&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="org.hsqldb.jdbcDriver" /&gt;
          &lt;property name="url" value="jdbc:hsqldb:mem:ex;sql.syntax_mys=true" /&gt;
          &lt;property name="username" value="sa" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jpaProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt;
          &lt;property name="properties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="jpaData" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaProperties" ref="jpaProperties" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="persistenceProvider" class="org.hibernate.ejb.HibernatePersistence" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>This problem seems really similar to the following question <a href="https://stackoverflow.com/questions/8230309/jpa-mapping-querysyntaxexception-foobar-is-not-mapped">JPA mapping: &quot;QuerySyntaxException: foobar is not mapped...&quot;</a> but the case is correct on my SELECT statement.</p>
  
  <p>What is interesting is that if I do a <code>entityManager.find(PlayerEntity.class, 1l);</code> then the saved entity is found.</p>
  
  <p>Does anyone have any ideas as to what the problem is?  I've spent quite some time on this and can't find a solution.</p>