我一直在玩一个非常简单的JPA示例,并试图将其调整到现有的数据库。但我无法克服这个错误。 (下)。它只是一些简单的东西,我没有看到。
org.hibernate.hql.internal.ast .QuerySyntaxException:FooBar未映射[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) $ c在下面的DocumentManager类(一个简单的servlet,因为这是我的目标目标)中有两件事情: > >插入效果很好 - 在那里都很好。问题在于检索。我已经尝试了 Query q = entityManager.createQuery 参数的各种值,但是没有运气,并且我尝试了更多类的更多重要注释(比如column类型),都没有成功。
请从我自己救我。我敢肯定,这是小事。我对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...其他获得者/设定者在这里}我的./src/ch/geekomatic/jpa/DocumentManager.java类
public class DocumentManager扩展HttpServlet { private EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(ch.geekomatic.jpa); 保护无效tearDown()抛出异常{ entityManagerFactory.close(); $ b @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); 坚持(文件); retrieveAll(response); } public void persist(FooBar document){ EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction()。begin(); entityManager.persist(document); entityManager.getTransaction()。commit(); entityManager.close(); $ b $ public void retrieveAll(HttpServletResponse response)throws IOException { EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction()。begin(); // ***问题行*** Query q = entityManager.createQuery(SELECT r FROM foobar r,FooBar.class); 列表< FooBar> result = q.getResultList(); (FooBar doc:result){ response.getOutputStream()。write(event.toString()。getBytes()); System.out.println(Document+ doc.getId()); } entityManager.getTransaction()。commit(); entityManager.close(); {b $ b} }{tomcat-home} / webapps /ROOT/WEB-INF/classes/METE-INF/persistance.xml文件
< persistence xmlns = java.sun/xml/ns/persistence xmlns:xsi =www.w3/2001/XMLSchema-instance xsi:schemaLocation = java.sun/xml/ns/persistence java.sun/xml/ns/persistence/persistence_2_0.xsd version =2.0> < persistence-unit name =ch.geekomatic.jpa> < description>测试直流的东西< / description> < class> ch.geekomatic.jpa.FooBar< / class> <属性> < property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/> < property name =javax.persistence.jdbc.urlvalue =jdbc:mysql:// svr:3306 / test/> < property name =javax.persistence.jdbc.uservalue =wafflesAreYummie/> < property name =javax.persistence.jdbc.passwordvalue =poniesRock/> < property name =hibernate.show_sqlvalue =true/> < property name =hibernate.hbm2ddl.autovalue =create/> < / properties> < / persistence-unit> < /余辉>MySQL表格描述:
mysql>描述foobar; + ------------ + -------------- + ------ + ----- + ---- ----- + ---------------- + |字段|类型|空| Key |默认|额外| + ------------ + -------------- + ------ + ----- + ---- ----- + ---------------- + | id | int(11)| NO | PRI | NULL | auto_increment | | rcpt_what | varchar(255)|是| | NULL | | | rcpt_where | varchar(255)|是| | NULL | | | rcpt_who | varchar(255)|是| | NULL | | + ------------ + -------------- + ------ + ----- + ---- ----- + ---------------- + 4行(0.00秒)解决方案
JPQL 主要不区分大小写。区分大小写的事情之一是Java实体名称。将您的查询改为:
SELECT r FROM FooBar r
I've been playing around with a very simple JPA example and am trying to tweak it to an existing database. 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:
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.
Please save me from myself. I'm certain it is something small. My inexperience with JPA is preventing me from going any farther.
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()mit(); 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()mit(); entityManager.close(); } }The {tomcat-home}/webapps/ROOT/WEB-INF/classes/METE-INF/persistance.xml file
<persistence xmlns="java.sun/xml/ns/persistence" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/persistence java.sun/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> 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. One of the things that is case-sensitive is Java entity names. Change your query to:
"SELECT r FROM FooBar r"
更多推荐
JPA映射:“QuerySyntaxException:foobar未映射...”
发布评论