在我的Hibernate代码中,我正在执行一个SQL命令。那里有一个LIKE
语句。代码如下。
public List<Object[]> searchProducts(String searchTerm, Session session) {
Query query = session.createSQLQuery("SELECT product_request_advertisement.idproduct_request_advertisement,\n"
+ " product_request_advertisement.user_iduser,\n"
+ " product_request_advertisement.product_idproduct,\n"
+ " product_request_advertisement.product_units_idproduct_units,\n"
+ " product_request_advertisement.type,\n"
+ " product_request_advertisement.grade,\n"
+ " product_request_advertisement.quantity,\n"
+ " product_request_advertisement.expected_price,\n"
+ " product_request_advertisement.required_place,\n"
+ " product_request_advertisement.required_date,\n"
+ " product_request_advertisement.extra_information,\n"
+ " product_request_advertisement.expire_on,\n"
+ " product_request_advertisement.delete_timestamp,\n"
+ " product_request_advertisement.date_created,\n"
+ " product_request_advertisement.last_updated,\n"
+ " user.name,\n"
+ " product_unit.unit_name\n"
+ " FROM product_request_advertisement\n"
+ " INNER JOIN user ON product_request_advertisement.user_iduser = user.iduser\n"
+ " INNER JOIN product_unit ON product_unit.idproduct_unit = product_request_advertisement.product_units_idproduct_units\n"
+ " INNER JOIN product ON product.idproduct = product_request_advertisement.product_idproduct\n"
+ " WHERE product_request_advertisement.delete_timestamp is null\n"
+ " AND product_request_advertisement.approved=true \n"
+ " AND product_request_advertisement.type LIKE '% :searchTerm %' or product.product_name LIKE '% :searchTerm %' \n"
+ " GROUP BY product_request_advertisement.idproduct_request_advertisement");
System.out.println("DAO SEARCHTERM: " + searchTerm);
query.setParameter("searchTerm", searchTerm);
List<Object[]> list = query.list();
return list;
}
执行此代码时,出现以下错误。
org.hibernate.QueryParameterException: could not locate named parameter [searchTerm]
at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:148)
at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:165)
at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:523)
at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:493)
at lk.maningmarket.api.dao.product_request_advertisement.ProductRequestAdvertisementDAOImpl.searchProducts(ProductRequestAdvertisementDAOImpl.java:206)
at lk.maningmarket.api.service.ProductRequestAdvertisementService.searchProducts(ProductRequestAdvertisementService.java:685)
at lk.maningmarket.api.rest.RequestAdvertisementJSONService.searchProducts(RequestAdvertisementJSONService.java:155)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:243)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
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:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
这可能是由于我在searchTerm
命令之间写了LIKE
的一种错误方式,但是我该如何解决呢?
答案 0 :(得分:3)
您应该使用以下LIKE
表达式:
LIKE :searchTerm
然后进行如下绑定:
query.setParameter("searchTerm", "%" + searchTerm "%");
这是您绑定一个字符串,该字符串即绑定了所需字符串之后的LIKE
表达式。