0

我在用

休眠:5.3

春天:5.0.5

我有这个查询:

select id,lat,lng, ST_distance_sphere(
                       ST_GeomFromText(CONCAT('POINT(',lat,' ',lng,')'), 4326),
                       ST_GeomFromText('POINT(36.287989 59.615465)', 4326)
   ) as distance from lots  where name like "%something%" having distance < 1

我想用hibernate执行这个,所以我有这个:

String query = "select id,lat,lng, ST_distance_sphere(\n" +
                    "ST_GeomFromText('POINT(36.287989 59.615465)', 4326),\n" +
                    "ST_GeomFromText('POINT(:lat :lng)', 4326)" +
                ") as distance from lots ";

        String where = "";
        where += " where name like :keyword";
        where += " having distance < :distance";

        query = query + where;
        System.out.println(query);
        Session session = sessionFactory.getCurrentSession();
        Query qlots = session.createNativeQuery(query);

        qlots = qlots.setParameter("keyword", "%"+keyword+"%");
        qlots = qlots.setParameter("lat", "36.287989");
        qlots = qlots.setParameter("lng", "59.615465");
        qlots = qlots.setParameter("distance", 2);

        List<Lot> lots = qlots.list();

这将返回一个错误:

java.lang.IllegalArgumentException: Could not locate named parameter [lat], expecting one of [distance, keyword]
    org.hibernate.query.internal.ParameterMetadataImpl.getNamedParameterDescriptor(ParameterMetadataImpl.java:217)
    org.hibernate.query.internal.ParameterMetadataImpl.getQueryParameter(ParameterMetadataImpl.java:186)
    org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:182)
    org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:494)
    org.hibernate.query.internal.NativeQueryImpl.setParameter(NativeQueryImpl.java:602)
    org.hibernate.query.internal.NativeQueryImpl.setParameter(NativeQueryImpl.java:57)
    com.controllers.api.LotController.lots(LotController.java:93)
    com.controllers.api.LotController$$FastClassBySpringCGLIB$$876a4b55.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
    com.controllers.api.LotController$$EnhancerBySpringCGLIB$$cfd997a8.lots(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.apache.catalina.filters.CorsFilter.handleNonCORS(CorsFilter.java:430)
    org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:169)

它说距离和关键字被称为参数,但 lat 和 lng 不是!我错过了什么可以在选择区域中定义参数吗?如果不是,我应该如何绑定这些参数?

编辑:这可能是因为 :lat 和 :lng 在引号中,而 Hibernate 将它们识别为字符串?

4

0 回答 0