org.hibernate.QueryException:*只允许在SELECT中的聚合函数内部

时间:2015-12-01 20:39:13

标签: java spring hibernate

当我尝试调用服务从远程数据库获取数据时出现此错误

控制台日志:

14:02:52,651 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/gasnaturalfenosa].[appServlet]] (http-localhost-127.0.0.1-8080-4) Servlet.service() para servlet appServlet lanzó excepción: java.lang.NullPointerException
    at com.gas.servicioImpl.ServicioEncuestaImpl.getEncuesta(ServicioEncuestaImpl.java:32) [classes:]
    at com.gas.rest.EncuestaController.getPueblos(EncuestaController.java:29) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_79]
    at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_79]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) [spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) [spring-webmvc-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230) [spring-orm-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106) [spring-web-3.2.13.RELEASE.jar:3.2.13.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

14:02:56,948 ERROR [stderr] (http-localhost-127.0.0.1-8080-4) org.hibernate.QueryException: * only allowed inside aggregate function in SELECT [SELECT *  FROM com.gas.modelo.Delegacion d ORDER BY d.delegacion ASC]

14:02:56,949 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.SelectParser.token(SelectParser.java:146)

14:02:56,949 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:109)

14:02:56,949 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.ClauseParser.end(ClauseParser.java:136)

14:02:56,950 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.PreprocessingParser.end(PreprocessingParser.java:145)

14:02:56,950 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:52)

14:02:56,950 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:240)

14:02:56,951 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:209)

14:02:56,951 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)

14:02:56,952 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)

14:02:56,952 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)

14:02:56,952 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)

14:02:56,953 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)

14:02:56,953 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)

14:02:56,953 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.gas.daoImpl.DelegacionDaoImpl.getDelegaciones(DelegacionDaoImpl.java:29)

14:02:56,954 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.gas.servicioImpl.ServicioDelegacionImpl.getDelegaciones(ServicioDelegacionImpl.java:30)

14:02:56,954 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.gas.rest.DelegacionController.getDelegaciones(DelegacionController.java:30)

14:02:56,954 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

14:02:56,955 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

14:02:56,955 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

14:02:56,955 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.reflect.Method.invoke(Method.java:606)

14:02:56,956 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)

14:02:56,956 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)

14:02:56,957 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)

14:02:56,957 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743)

14:02:56,958 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672)

14:02:56,958 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82)

14:02:56,959 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)

14:02:56,959 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)

14:02:56,959 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)

14:02:56,960 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)

14:02:56,960 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

14:02:56,961 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)

14:02:56,961 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

14:02:56,962 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

14:02:56,962 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

14:02:56,962 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230)

14:02:56,963 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)

14:02:56,963 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

14:02:56,964 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

14:02:56,964 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

14:02:56,965 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

14:02:56,965 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

14:02:56,966 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

14:02:56,966 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

14:02:56,967 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

14:02:56,967 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

14:02:56,967 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

14:02:56,968 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

14:02:56,968 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

14:02:56,968 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.Thread.run(Thread.java:745)

以下是我查询的方式:

@Repository
public class DelegacionDaoImpl  implements DelegacionDao {

    @Autowired
    @Qualifier("sfGas")
    SessionFactory sf;

    Session session;    
    @SuppressWarnings("unchecked")
    public List<Delegacion> getDelegaciones(){
        try{
            Session session = sf.getCurrentSession();
            Query q = session.createQuery("SELECT *  FROM Delegacion d ORDER BY d.delegacion ASC");
            return (List<Delegacion>) q.list();
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

道:

@Entity
@Table(name="catDelegacion")
public class Delegacion implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id_delegacion")
    private Integer id_delegacion;

    @Column(name="delegacion")
    private String delegacion;

    public Integer getId_delegacion() {
        return id_delegacion;
    }

    public void setId_delegacion(Integer id_delegacion) {
        this.id_delegacion = id_delegacion;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }




    public String getDelegacion() {
        return delegacion;
    }

    public void setDelegacion(String delegacion) {
        this.delegacion = delegacion;
    }
}

为什么我收到此错误?我做错了什么?在我的春季项目中,其他一切都很好。

修改 现在我收到了这个错误

15:30:41,835 WARN  [org.hibernate.util.JDBCExceptionReporter] (http-localhost-127.0.0.1-8080-4) SQL Error: 0, SQLState: 42P01
15:30:41,836 ERROR [org.hibernate.util.JDBCExceptionReporter] (http-localhost-127.0.0.1-8080-4) ERROR: relation "catdelegacion" does not exist
  Position: 91
15:30:41,836 ERROR [stderr] (http-localhost-127.0.0.1-8080-4) org.hibernate.exception.SQLGrammarException: could not execute query

没有catdelegacion,但 catDelegacion

1 个答案:

答案 0 :(得分:3)

这是Hibernate,查询语言是HQL,它与SQL类似但不完全相同。如果您想要返回完整的Delegacion个对象,请说明

FROM Delegacion d ORDER BY d.delegacion ASC

即。没有select *。 有关HQL的更多信息,请查看https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-from