我使用下面的代码(试图)查询数据库:
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?