gpt4 book ai didi

java - 如何使用jpa中的选择查询来检索所有数据?

转载 作者:行者123 更新时间:2023-12-02 08:44:58 24 4
gpt4 key购买 nike

我有以下代码:

    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

最佳答案

正确的 JPQL 查询为 SELECT a FROM Author a如果作者是实体的名称。或者简单地FROM Author也适用于您的情况。

关于java - 如何使用jpa中的选择查询来检索所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61141109/

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