Sql 为什么setParameter不设置参数?

Sql 为什么setParameter不设置参数?,sql,hibernate,parameters,hql,Sql,Hibernate,Parameters,Hql,我正在使用以下代码(尝试)查询数据库: Query query = session.createQuery("from Trace where service = :service"); query.setParameter("service", clientRequest[0]); 其中clientRequest[0]来自字符串数组,而服务变量是映射到MySQL数据库中的VARCHAR(45)的POJO中的字符串 运行此代码时,Hibernate将执行的SQL查询显示为: Hibernate

我正在使用以下代码(尝试)查询数据库:

Query query = session.createQuery("from Trace where service = :service");
query.setParameter("service", clientRequest[0]);
其中clientRequest[0]来自字符串数组,而服务变量是映射到MySQL数据库中的VARCHAR(45)的POJO中的字符串

运行此代码时,Hibernate将执行的SQL查询显示为:

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=?
这使我相信clientRequest[0]的值没有正确设置为参数

我已检查clientRequest[0]是否包含有效字符串,它确实包含有效字符串。我尝试过其他创建查询的方法,但都不起作用,它们总是将服务显示为“?”,而如果我使用明显的:

Query query = session.createQuery("from Trace where service = 21");
它自然会给出正确的响应

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

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

这是日志中的预期输出。Hibernate不在查询字符串中记录参数。参数设置正确。当未设置参数时,您将看到异常


如果您还想记录参数值,可以从中找到一些说明。

Awesome。在我验证之前,我会检查并做一些测试来确认。非常感谢。刚刚确认参数添加正确,我现在无法让日志工作,但您的回答完全正确。再次感谢!