[英]JPA mapping: "QuerySyntaxException: foobar is not mapped..."
I've been playing around with a very simple JPA example and am trying to tweak it to an existing database.我一直在玩一个非常简单的 JPA 示例,并试图将其调整为现有数据库。 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:在下面的 DocumentManager class(一个简单的 servlet,因为这是我的目标)中做了两件事:
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.我已经尝试了
Query q = entityManager.createQuery
参数的各种值,但没有成功,并且我尝试了 class 的各种更详细的注释(如列类型),但都没有成功。
Please save me from myself.请救我脱离自我。 I'm certain it is something small.
我敢肯定这是小事。 My inexperience with JPA is preventing me from going any farther.
我对 JPA 的缺乏经验阻止了我走得更远。
My./src/ch/geekomatic/jpa/FooBar.java file:我的./src/ch/geekomatic/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
}
My./src/ch/geekomatic/jpa/DocumentManager.java class我的./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();
}
}
The {tomcat-home}/webapps/ROOT/WEB-INF/classes/METE-INF/persistance.xml 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>
The MySQL table description: 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)
JPQL mostly is case-insensitive. JPQL大多不区分大小写。 One of the things that is case-sensitive is Java entity names.
区分大小写的事情之一是 Java 实体名称。 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
在某些 J2EE / web 容器中(根据我在 Jboss 7.x 和 Tomcat 7.x 下的经验)您必须将要用作休眠实体的每个类添加到文件persistence.xml中
<class>com.yourCompanyName.WhateverEntityClass</class>
In case of jboss this concerns every entity class (local - ie within the project You are developing or in a library).在 jboss 的情况下,这涉及每个实体类(本地 - 即在您正在开发的项目中或在库中)。 In case of Tomcat 7.x this concerns only entity classes within libraries.
对于 Tomcat 7.x,这仅涉及库中的实体类。
You have declared your Class as:您已将您的班级声明为:
@Table( name = "foobar" )
public class FooBar {
You need to write the Class Name for the search.您需要为搜索编写类名。
from FooBar
来自
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我在使用其他一个实体时遇到了同样的错误,他通过在 @Entity 注释中使用表名而不使用 @Table 注释错误地注释了类
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与您的问题无关,但是一旦我遇到 JPA 映射:“QuerySyntaxException:foobar 未映射...问题,因为我添加了
@Entity(name="foo")
class Foobar{
...
}
and tried to refer Foobar并试图参考 Foobar
Could also be the annotation provider, for newer versions of hibernate they use jakarta.persistence.* and not javax.persistence.*也可以是注释提供者,对于 hibernate 的较新版本,他们使用 jakarta.persistence.* 而不是 javax.persistence.*
HOPE IT DOESN'T TAKE HOURS OF HEAD WALLING TO FIGURE THAT OUT希望它不需要花费数小时的时间来解决这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.