gpt4 book ai didi

java - JPA 映射 : "QuerySyntaxException: foobar is not mapped..."

转载 作者:IT老高 更新时间:2023-10-28 12:53:32 25 4
gpt4 key购买 nike

我一直在玩一个非常简单的 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)

最佳答案

JPQL 大部分不区分大小写。 Java 实体名称是区分大小写的一件事。将您的查询更改为:

"SELECT r FROM FooBar r"

关于java - JPA 映射 : "QuerySyntaxException: foobar is not mapped...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com