为什么不的setParameter设置的参数?(Why does setParameter not

2019-07-29 20:49发布

我使用下面的代码(试图)查询数据库:

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

其中的ClientRequest [0]是从一个字符串数组和服务变量是在我在我的MySQL数据库POJO映射到VARCHAR(45)的字符串。

当我运行这段代码时,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

什么可能无法正常工作被停止setParamater?

Answer 1:

预计在日志输出。 Hibernate不登录查询字符串参数。 参数设置是否正确。 如果没有设置参数,您会看到异常,而不是。

如果你也想记录的参数值,一些指令可以被发现这里 。



文章来源: Why does setParameter not set the parameter?