如何在JPA中使用SELECT查询检索所有数据?

huangapple go评论75阅读模式
英文:

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(&quot;select * from author_lab &quot;);
List&lt;Author&gt; list = (List&lt;Author&gt;) query.getResultList();
for (Author a : list) {
System.out.println(&quot;ID : &quot; + a.getAuthorId());
System.out.println(&quot;First Name : &quot; + a.getFirstname());
System.out.println(&quot;Middle Name : &quot; + a.getMiddleName());
System.out.println(&quot;Last Name : &quot; + a.getLastName());
}

Which return the following error:

1890 [main] ERROR org.hibernate.hql.PARSER - line 1:8: unexpected token: *
Exception in thread &quot;main&quot; 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.&lt;init&gt;(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.&lt;init&gt;(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.

huangapple
  • 本文由 发表于 2020年4月10日 21:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定