JPA 映射: “ QuerySyntaxException: foobar 没有映射... ...”

我一直在使用一个非常简单的 JPA 示例,并试图将其调整到现有的数据库中。但我无法通过这个错误。(下)只是一些我看不到的简单的东西。

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)

在下面的 DocumentManager 类中(一个简单的 servlet,因为这是我的目标)做两件事:

  1. 插入一行
  2. 返回所有行

插入完全正常,一切正常。问题在于检索。我已经尝试了 Query q = entityManager.createQuery参数的所有类型的值,但没有成功,我还尝试了类的各种更明确的注释(如列类型) ,但都没有成功。

请救救我。我肯定是个小东西。我对 JPA 的缺乏经验使我无法继续前进。

我的./src/ch/geekomic/jpa/FooBar.java 文件:

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

我的./src/ch/geekomic/jpa/DocumentManager.java 类

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();
}
}

{ tomcat-home }/webapps/ROOT/WEB-INF/classes/METE-INF/durance.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 表描述:

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)
128227 次浏览

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"

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

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

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.

You have declared your Class as:

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

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

I got the same error while using other one entity, He was annotating the class wrongly by using the table name inside the @Entity annotation without using the @Table annotation

The correct format should be

@Entity //default name similar to class name 'FooBar' OR @Entity( name = "foobar" ) for differnt entity name
@Table( name = "foobar" ) // Table name
public class FooBar{

Not related to your issue but once I faced JPA mapping: "QuerySyntaxException: foobar is not mapped... issue because I added

@Entity(name="foo")
class Foobar{
...
}

and tried to refer Foobar

Could also be the annotation provider, for newer versions of hibernate they use jakarta.persistence.* and not javax.persistence.*

HOPE IT DOESN'T TAKE HOURS OF HEAD WALLING TO FIGURE THAT OUT