JPA mapping: “QuerySyntaxException: foobar is not mapped…”

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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.

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) 

In the DocumentManager class below (a simple servlet, as that is my target goal) does two things:

  1. insert a row
  2. return all rows

The insertion works perfectly--all is good there. The problem is with the retrieval. I've tried all sorts of values for the Query q = entityManager.createQuery parameters, but no luck, and I've tried variously more explicate annotations of the class (like column types), all without success.

Please save me from myself. I'm certain it is something small. My inexperience with JPA is preventing me from going any farther.

My ./src/ch/geekomatic/jpa/FooBar.java file:

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

My ./src/ch/geekomatic/jpa/DocumentManager.java class

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 result = q.getResultList();          for ( FooBar doc : result ) {             response.getOutputStream().write(event.toString().getBytes());             System.out.println( "Document " + doc.getId()  );         }         entityManager.getTransaction().commit();         entityManager.close();     } } 

The {tomcat-home}/webapps/ROOT/WEB-INF/classes/METE-INF/persistance.xml file

test stuff for dcch.geekomatic.jpa.FooBar

The MySQL table description:

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

回答1:

JPQL mostly is case-insensitive. One of the things that is case-sensitive is Java entity names. Change your query to:

"SELECT r FROM FooBar r" 


回答2:

There is also another possible source of this error. In some J2EE / web containers (in my experience under Jboss 7.x and Tomcat 7.x) You have to add each class You want to use as a hibernate Entity into the file persistence.xml as

com.yourCompanyName.WhateverEntityClass

In case of jboss this concerns every entity class (local - i.e. within the project You are developing or in a library). In case of Tomcat 7.x this concerns only entity classes within libraries.



回答3:

You have declared your Class as:

@Table( name = "foobar" ) public class FooBar { 

You need to write the Class Name for the search.
from FooBar



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!