JPA映射:“ QuerySyntaxException:未映射foobar…”


72

我一直在研究一个非常简单的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/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
}

我的./src/ch/geekomatic/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/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表说明:

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)

Answers:


179

JPQL通常不区分大小写。区分大小写的事情之一是Java实体名称。将查询更改为:

"SELECT r FROM FooBar r"

7
那么,是类名而不是表名?
Stu Thompson

33
是。尽管它看起来像sql,但是您正在执行的JPA查询语言;因此您是从实体而不是表格中进行选择。
克里斯,

7
你一定是在跟我开玩笑!我只花了2个小时就将配置文件分开,检查类路径并重新设计构建文件。我阅读了您的文章,并将“ tableName”更改为“ TableName”,它起作用了!
CodeClimber

我花了太多时间寻找这个答案。如果我找到你,就在我身上喝酒,互联网人。:)
janoulle

1
我不知道该如何用语言感谢你!我在哭泣
Vishwa Ardeshna '19

14

此错误还有另一个可能的来源。在某些J2EE / Web容器中(以我在Jboss 7.x和Tomcat 7.x下的经验),您必须将要用作休眠实体的每个类添加到文件persistence.xml中,如下所示:

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

如果是jboss,则涉及每个实体类(本地-即在您正在开发的项目中或在库中)。对于Tomcat 7.x,这仅涉及库中的实体类。


5

您已将课程声明为:

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

您需要为搜索编写“类名”。
FooBar


0

我在使用其他一个实体时遇到了相同的错误,他通过使用@Entity注释内的表名而不使用@Table注释错误地注释了类

正确的格式应为

@Entity //default name similar to class name 'FooBar' OR @Entity( name = "foobar" ) for differnt entity name
@Table( name = "foobar" ) // Table name 
public class FooBar{
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.