为什么setParameter没有设置参数

为什么setParameter没有设置参数

本文介绍了为什么setParameter没有设置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 查询查询= session.createQuery( 来自Trace where service =:service); 
query.setParameter(service,clientRequest [0]);

其中clientRequest [0]来自字符串数组,服务变量是我POJO中的字符串映射到我的MySQL数据库中的VARCHAR(45)。



当我运行这段代码时,Hibernate显示执行的SQL查询为:

 休眠:选择trace0_.traceId作为traceId0_,trace0_.service作为service0_,trace0_.longitude作为longitude0_,trace0_.latitude作为latitude0_,trace0_.timestamp作为timestamp0_从跟踪trace0_其中trace0_.service =? 

这导致我相信clientRequest [0]的值没有被正确设置为参数。

我已经检查过clientRequest [0]包含一个有效的String,它会这样做。我尝试了其他创建查询的方式,但其中没有一个能够工作,它们总是将服务显示为'?',而如果我使用明显的:

  Query query = session.createQuery(from Trace where service = 21); 

它自然会给出正确的回应



<$休眠:选择trace0_.traceId作为traceId0_,trace0_.service作为service0_,trace0_.longitude作为longitude0_,trace0_.latitude作为latitude0_,trace0_.timestamp作为timestamp0_ from trace trace0_,其中trace0_.service = 21

什么可能会阻止setParamater正常工作?

解决方案

预计在日志中输出。 Hibernate不会在查询字符串中记录参数。参数设置正确。如果您还想记录参数的值,可以从。


I'm using the following code to (attempt to) query a database:

Query query = session.createQuery("from Trace where service = :service");
query.setParameter("service", clientRequest[0]);

Where clientRequest[0] is from an array of Strings and the service variable is a String in my POJO mapped to a VARCHAR(45) in my MySQL database.

When I run this code, Hibernate shows the executed SQL query as:

Hibernate: select trace0_.traceId as traceId0_, trace0_.service as service0_, trace0_.longitude as longitude0_, trace0_.latitude as latitude0_, trace0_.timestamp as timestamp0_ from trace trace0_ where trace0_.service=?

Which leads me to believe that the value of clientRequest[0] is not being set correctly as a parameter.

I have checked that clientRequest[0] contains a valid String, which it does. I've tried other ways of creating a query but none of these have worked, they always show service as '?', while if I use the obvious:

Query query = session.createQuery("from Trace where service = 21");

it naturally gives the correct response of

Hibernate: select trace0_.traceId as traceId0_, trace0_.service as service0_, trace0_.longitude as longitude0_, trace0_.latitude as latitude0_, trace0_.timestamp as timestamp0_ from trace trace0_ where trace0_.service=21

What might be stopping setParamater from working correctly?

解决方案

It is expected output in logs. Hibernate does not log parameters in query strings. Parameter is correctly set. When parameter is not set, you see exception instead.

If you want to log also values of parameters, some instructions can be found from here.

这篇关于为什么setParameter没有设置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:05