英文:
how to work with select query in jpa for retrieving all the data?
问题
我有以下代码:
Query query = entitytManager.createQuery("select * from author_lab ");
List<Author> list = (List<Author>) query.getResultList();
for (Author a : list) {
System.out.println("ID : " + a.getAuthorId());
System.out.println("First Name : " + a.getFirstname());
System.out.println("Middle Name : " + a.getMiddleName());
System.out.println("Last Name : " + a.getLastName());
}
这段代码返回以下错误:
1890 [main] ERROR org.hibernate.hql.PARSER - line 1:8: unexpected token: *
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from author_lab ]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1166)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1112)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:266)
at com.cg.client.Client.main(Client.java:55)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from author_lab ]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:263)
... 1 more
英文:
I have the following code:
Query query = entitytManager.createQuery("select * from author_lab ");
List<Author> list = (List<Author>) query.getResultList();
for (Author a : list) {
System.out.println("ID : " + a.getAuthorId());
System.out.println("First Name : " + a.getFirstname());
System.out.println("Middle Name : " + a.getMiddleName());
System.out.println("Last Name : " + a.getLastName());
}
Which return the following error:
1890 [main] ERROR org.hibernate.hql.PARSER - line 1:8: unexpected token: *
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from author_lab ]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1166)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1112)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:266)
at com.cg.client.Client.main(Client.java:55)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from author_lab ]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:263)
... 1 more
答案1
得分: 1
正确的JPQL查询应该是如果"Author"是实体的名称,则为SELECT a FROM Author a
。或者在您的情况下,简单地使用FROM Author
也可以。
英文:
The correct JPQL query would be SELECT a FROM Author a
if Author was the name of the entity. Or simply FROM Author
would also work in your case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论