JPA: "QuerySyntaxException: foobar is not displayed ..."

I played with a very simple JPA example and am trying to tune it to an existing database. But I can’t get past this error. (Below). It just has to be a simple thing that I don't see.

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 class below, the DocumentManager (a simple servlet, since this is my target) has two functions:

  • add line
  • return all rows

The insert works fine - everything is fine there. The problem with the search. I tried all kinds of values ​​for the Query q = entityManager.createQuery parameters Query q = entityManager.createQuery , but no luck, and I tried to explain differently the class annotations (for example, column types) in more detail, all without success.

Please save me from yourself. I am sure this is something small. My inexperience with JPA is stopping me from moving on.

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

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

 <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"> <persistence-unit name="ch.geekomatic.jpa"> <description>test stuff for dc</description> <class>ch.geekomatic.jpa.FooBar</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://svr:3306/test" /> <property name="javax.persistence.jdbc.user" value="wafflesAreYummie" /> <property name="javax.persistence.jdbc.password" value="poniesRock" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence> 

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) 
+43
java mysql hibernate jpa
Nov 22 '11 at 16:37
source share
3 answers

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

 "SELECT r FROM FooBar r" 
+116
Nov 22 '11 at 16:45
source share

There is another possible source of this error. In some J2EE / web containers (in my experience with Jboss 7.x and Tomcat 7.x) you have to add each class that you want to use as a sleeping Entity in the persistence file . xml like

<class>com.yourCompanyName.WhateverEntityClass</class>

In the case of jboss, this applies to each entity class (local - that is, inside the project that you are developing or in the library). In the case of Tomcat 7.x, this applies only to object classes in libraries.

+14
Apr 18 '14 at 8:40
source share

You declared your class as:

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

You need to write a class name to search.
from FooBar

+3
Dec 06 '16 at 23:41
source share



All Articles