问题描述
我在Spring引导中使用hql查询从数据库表中获取记录。我使用@ResponseBody返回json对象的结果。我只获取数据但没有获取列名,所以我无法了解包含哪些数据的列。
这是我的 DAO
@Transactional
public interface MyDaoInterface extends CrudRepository< Rating,Long> {
@Query( countByPostId)
List< Rating> countByPostId(Long post_id);
final String countByPostId =select new netgloo.models.CountAndPoints(count(r.id),r.points))from Rating r where r.postId =?1 group by r.points;
$ b 这是我的控制器
@RequestMapping(value =/ rating / {id},produce =application / json)
@ ResponseBody
public List< Rating> getRatingInfo(@PathVariable(id)long id,HttpServletRequest req,HttpServletResponse res)throws Exception {
List< Rating> postobj = myDao.countByPostId(id);
返回postobj;
$ / code>
获取像
的数据
[
{
count:3,
points:1
},
{
count:2,
分:2
},
{
count:4,
分:3
}
]
但我想要
result:[
{
count:3,
points:1
},
$ bcount:2,
points:2
},
{
count:4,
points :3
}
]
新课程 CountAndPoints
package netgloo.models;
公共类CountAndPoints {
私人长计数;
私人长分;
public long getCount(){
return count;
}
public void setCount(long count){
this.count = count;
}
public long getPoints(){
return points;
}
public void setPoints(长分){
this.points = points;
}
}
异常
线程main中的异常org.springframework.beans.factory.BeanCreationException:创建名为'mainController'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private netgloo.models.MyDaoInterface netgloo.controllers.MainController.myDao;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名为'myDaoInterface'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:验证方法的查询失败public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230 )
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.Abstra在org.springframework.support.AbstractApplicationContext中
。 finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh( EmbeddedWebApplicationContext.java:118)在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686
)
在org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
。在org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
在org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
在netgloo.Application.main (Application.java:25)
由org.springframework引起.beans.factory.BeanCreationException:无法自动装入字段:private netgloo.models.MyDaoInterface netgloo.controllers.MainController.myDao;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名为'myDaoInterface'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:验证方法的查询失败public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java: 88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 more
引起:org.springframework.beans.factory .BeanCreationException:创建名为'myDaoInterface'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:验证方法的查询失败public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject
(AbstractBeanFactory.java:303 )
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299 )
处org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
(DefaultLi stableBeanFactory.java:1120)在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044
)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 18 more
由java导致: .lang.IllegalArgumentException:验证方法的查询失败public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!在org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
。在org.springframework.data.jpa.repository.query.SimpleJpaQuery<初始化> (SimpleJpaQuery.java:62)
在org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
在org.springframework.data.jpa.repository.query .JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:135)
在org.springframework.data .jpa.repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:203)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExec utorMethodInterceptor<初始化>(RepositoryFactorySupport.java:349)
。在org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:187)
。在org.springframework.data .repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
。在组织.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 28 more
引起:java.lang.IllegalArgumentException:org.hibernate.hql。 internal.ast.QuerySyntaxException:意外的子树结束[从netgloo.models.Rating r中选择新的netgloo.models.CountAndPoints(count(r.id),r.points))r其中r.postId =?1组由r.points ]在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
。在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
。在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
处太阳org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
。 reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:483)
在org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocat ionHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy。$ Proxy63.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery( SimpleJpaQuery.java:86)
... 41 more
引起:org.hibernate.hql.internal.ast.QuerySyntaxException:意外的子树结束[select new netgloo.models.CountAndPoints(count(r .id),r.points))from netgloo.models.Rating r where r.postId =?1 group by r.points]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException。 Java的:91)
在org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
在org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl。的java:在org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl在org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile 284)
(QueryTranslatorImpl.java:206)
。 java:158)
在org.hibernate.engine.query.spi.HQLQue (HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan。< init>(HQLQueryPlan.java:93)
at org.hibernate。 engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
处org.hibernate.internal.AbstractSessionImpl org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
。的createQuery(AbstractSessionImpl.java:236)
在org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
在org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java: 328)
... 48 more
解决方案 @Query(countByPostId)
List< Rating> countByPostId(Long post_id);
最终字符串countByPostId =select count(r.id),r.points from Rating r where r.postId =?1 group by r.points;
这没什么意义。您的查询不会返回评级实例列表。它返回一个Object数组列表,其中每个数组包含2个元素:一个计数和一些点数。
Rating类甚至没有包含计数的字段无论如何。
您需要一些包含这两个字段的类以及一个返回此类实例的查询:
从评级r中选择新的com.foo.bar.CountAndPoints(count(r.id),r.points),其中r.postId =?1组由r.points
I am getting records from database table using hql query in spring boot. I am returning result in json object using @ResponseBody. I am getting only data but not getting column names so i am unable to understand which column containg which data.
Here is my DAO
@Transactional
public interface MyDaoInterface extends CrudRepository<Rating, Long>{
@Query(countByPostId)
List<Rating> countByPostId(Long post_id);
final String countByPostId= "select new netgloo.models.CountAndPoints(count(r.id), r.points)) from Rating r where r.postId = ?1 group by r.points";
}
Here is my controller
@RequestMapping(value = "/rating/{id}", produces="application/json")
@ResponseBody
public List<Rating> getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
List<Rating> postobj = myDao.countByPostId(id);
return postobj;
}
Getting data like
[
{
"count": 3,
"points": 1
},
{
"count": 2,
"points": 2
},
{
"count": 4,
"points": 3
}
]
But i want like
"result:"[
{
"count": 3,
"points": 1
},
{
"count": 2,
"points": 2
},
{
"count": 4,
"points": 3
}
]
New class CountAndPoints
package netgloo.models;
public class CountAndPoints {
private long count;
private long points;
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public long getPoints() {
return points;
}
public void setPoints(long points) {
this.points = points;
}
}
Exception
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private netgloo.models.MyDaoInterface netgloo.controllers.MainController.myDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDaoInterface': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at netgloo.Application.main(Application.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private netgloo.models.MyDaoInterface netgloo.controllers.MainController.myDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDaoInterface': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDaoInterface': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 18 more
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List netgloo.models.MyDaoInterface.countByPostId(java.lang.Long)!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:135)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:203)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:349)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:187)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 28 more
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [select new netgloo.models.CountAndPoints(count(r.id), r.points)) from netgloo.models.Rating r where r.postId = ?1 group by r.points]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy.$Proxy63.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
... 41 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [select new netgloo.models.CountAndPoints(count(r.id), r.points)) from netgloo.models.Rating r where r.postId = ?1 group by r.points]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:284)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
... 48 more
解决方案 @Query(countByPostId)
List<Rating> countByPostId(Long post_id);
final String countByPostId= "select count(r.id), r.points from Rating r where r.postId=?1 group by r.points";
This doesn't make much sense. Your query does not return a list of Rating instances. It returns a list of Object arrays, where each array contains 2 elements: a count and a number of points.
The Rating class doesn't even have a field containing counts anyway.
You need some class that contains these two fields, and a query that returns instances of this class:
select new com.foo.bar.CountAndPoints(count(r.id), r.points) from Rating r where r.postId = ?1 group by r.points"
这篇关于没有获得hql查询结果中的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!