title: JPA mapping: "QuerySyntaxException: foobar is not mapped..."
body: <p>I've been playing around with a very simple JPA example and am trying to tweak it to an existing database.  But I can't get past this error.  (Below.)  It just has to be some simple thing I am not seeing.</p>
  
  <pre><code>org.hibernate.hql.internal.ast.QuerySyntaxException: FooBar is not mapped [SELECT r FROM FooBar r]
    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)
  </code></pre>
  
  <p>In the DocumentManager class below (a simple servlet, as that is my target goal) does two things:</p>
  
  <ol>
  <li>insert a row</li>
  <li>return all rows</li>
  </ol>
  
  <p>The insertion works perfectly--all is good there.  The problem is with the retrieval.  I've tried all sorts of values for the <code>Query q = entityManager.createQuery</code> parameters, but no luck, and I've tried variously more explicate annotations of the class (like column types), all without success.</p>
  
  <p>Please save me from myself.  I'm certain it is something small.  My inexperience with JPA is preventing me from going any farther.</p>
  
  <p><strong>My ./src/ch/geekomatic/jpa/FooBar.java file:</strong></p>
  
  <pre><code>@Entity
  @Table( name = "foobar" )
  public class FooBar {
      @Id 
      @GeneratedValue(strategy=GenerationType.IDENTITY) 
      @Column(name="id")
      private int id;
  
      @Column(name="rcpt_who")
      private String rcpt_who;
  
      @Column(name="rcpt_what")
      private String rcpt_what;
  
      @Column(name="rcpt_where")
      private String rcpt_where;
  
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
  
      public String getRcpt_who() {
          return rcpt_who;
      }
      public void setRcpt_who(String rcpt_who) {
          this.rcpt_who = rcpt_who;
      }
  
      //snip...the other getters/setters are here
  }
  </code></pre>
  
  <p><strong>My ./src/ch/geekomatic/jpa/DocumentManager.java class</strong></p>
  
  <pre><code>public class DocumentManager extends HttpServlet {
      private EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "ch.geekomatic.jpa" );
  
      protected void tearDown() throws Exception {
          entityManagerFactory.close();
      }
  
     @Override
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
         FooBar document = new FooBar();
         document.setRcpt_what("my what");
         document.setRcpt_who("my who");
  
         persist(document);
  
         retrieveAll(response);
     }
  
     public void persist(FooBar document) {
         EntityManager entityManager = entityManagerFactory.createEntityManager();
         entityManager.getTransaction().begin();
         entityManager.persist( document );
         entityManager.getTransaction().commit();
         entityManager.close();
     }
  
      public void retrieveAll(HttpServletResponse response) throws IOException {
          EntityManager entityManager = entityManagerFactory.createEntityManager();
          entityManager.getTransaction().begin();
  
          //  *** PROBLEM LINE ***
          Query q = entityManager.createQuery( "SELECT r FROM foobar r", FooBar.class );
          List&lt;FooBar&gt; result = q.getResultList();
  
          for ( FooBar doc : result ) {
              response.getOutputStream().write(event.toString().getBytes());
              System.out.println( "Document " + doc.getId()  );
          }
          entityManager.getTransaction().commit();
          entityManager.close();
      }
  }
  </code></pre>
  
  <p><strong>The {tomcat-home}/webapps/ROOT/WEB-INF/classes/METE-INF/persistance.xml file</strong></p>
  
  <pre><code>&lt;persistence 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_2_0.xsd"
      version="2.0"&gt;
  
  &lt;persistence-unit name="ch.geekomatic.jpa"&gt;
      &lt;description&gt;test stuff for dc&lt;/description&gt;
  
      &lt;class&gt;ch.geekomatic.jpa.FooBar&lt;/class&gt;
  
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.driver"   value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="javax.persistence.jdbc.url"      value="jdbc:mysql://svr:3306/test" /&gt;
          &lt;property name="javax.persistence.jdbc.user"     value="wafflesAreYummie" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="poniesRock" /&gt;
  
          &lt;property name="hibernate.show_sql"     value="true" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="create" /&gt;
      &lt;/properties&gt;
  
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>The MySQL table description:</strong></p>
  
  <pre><code>mysql&gt; describe foobar;
  +------------+--------------+------+-----+---------+----------------+
  | Field      | Type         | Null | Key | Default | Extra          |
  +------------+--------------+------+-----+---------+----------------+
  | id         | int(11)      | NO   | PRI | NULL    | auto_increment |
  | rcpt_what  | varchar(255) | YES  |     | NULL    |                |
  | rcpt_where | varchar(255) | YES  |     | NULL    |                |
  | rcpt_who   | varchar(255) | YES  |     | NULL    |                |
  +------------+--------------+------+-----+---------+----------------+
  4 rows in set (0.00 sec)
  </code></pre>