gpt4 book ai didi

java - 具有双参数的 JPA Criteria api 函数

转载 作者:行者123 更新时间:2023-11-30 02:31:43 25 4
gpt4 key购买 nike

我被这个问题困扰了,在网上搜索了两天后,你是我最后的希望:p

我正在使用 JPA criteria api 来构建我的查询。一切工作正常,除非我想添加一些 sql 函数。

这就是我使用 sql 函数所做的:
表达式 makePoint = criteriaBuilder.function("ST_MakePoint", Geometry.class, criteriaBuilder.parameter(double.class, "lat"), criteriaBuilder.parameter(double.class, "lng"));
表达式 distanceSphere = criteriaBuilder.function("ST_Distance_Sphere", double.class, place.get("坐标"), makePoint);
表达式 = criteriaBuilder.and(表达式, criteriaBuilder.lessThanOrEqualTo(distanceSphere, radius));
criteriaQuery.select(root).where(表达式);

声明工作正常,但是当我设置参数时:
session.createQuery(criteriaQuery).setParameter("lat",lat)

我有这个错误:

java.lang.IllegalArgumentException: Named parameter [lat] type mismatch; expecting [double] but found [Double]
at org.hibernate.query.criteria.internal.compile.ExplicitParameterInfo.validateBindValue(ExplicitParameterInfo.java:70)
at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:467)
at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.setParameter(CriteriaQueryTypeQueryAdapter.java:57)
at co.sumwhere.api.communities.communitiesToPlaces.CommunitiesToPlaces.researchMultiple(CommunitiesToPlaces.java:207)
at co.sumwhere.api.places.PlacesController.getPlaces(PlacesController.java:356)
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:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1368)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

我什至尝试使用以下方式设置参数:
双纬度 = 12.56;
双纬度 = Double.parseDouble(“12.552”);

没有任何效果:(

最佳答案

更改类型

criteriaBuilder.parameter(double.class, "lat"),

criteriaBuilder.parameter(Double.class, "lat"),

应该适合你。进一步阅读refer 。另请运行以下示例以了解您看到的异常。

import java.util.ArrayList;
import java.util.List;

public class Main {

public static void main(String args[]) {
List<Double> list = new ArrayList<>();
double var = 1022.222;
list.add(var);
// Notice that the double is autoboxed to Double type.
System.out.println(list.get(0).getClass());
System.out.println(double.class);
// System.out.println(list.get(0).getClass().equals(double.class));
}
}

关于java - 具有双参数的 JPA Criteria api 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149546/

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