Why does setParameter not set the parameter?

感情迁移 提交于 2019-12-02 02:12:42

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10984579/why-does-setparameter-not-set-the-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!