简体   繁体   English

如果查询是动态语句,如何在hibernate中绑定查询参数?

[英]How to bind query parameters in hibernate if query is dynamic statement?

I am having a dynamic query as : 我有一个动态查询:

DECLARE @LOERangeMin int =0 ;
       DECLARE @LOERangeMax int = 0;
       DECLARE @CycleStartStatus nvarchar(500) = :param1;
       DECLARE @CycleEndStatus nvarchar(500) = :param2;
       DECLARE @Team nvarchar(100) = NULL;
       DECLARE @ReportStartDate datetime = NULL;
       DECLARE @ReportEndDate datetime = GETDATE();

select min([Cycle Time]) AS AvgMinCycleTime,max([Cycle Time]) AS AvgMaxCycleTime,AVG([Cycle Time]) AS AvgCycleTime
from EpicTable where EpicStartStatusName = @CycleStartStatus  and EpicEndStatusName = @CycleEndStatus

I am preparing Query object by passing this query as a string as : 我通过将此查询作为字符串传递来准备Query对象:

Query que = session.createSQLQuery(queryString);

In this I want to set CycleStartStatus and CycleEndStatus using que.setParameter("param1",CycleStartStatus); 在这里我想使用que.setParameter(“param1”,CycleStartStatus)设置CycleStartStatus和CycleEndStatus; and que.setParameter("param2",CycleEndStatus); 和que.setParameter(“param2”,CycleEndStatus);

I tried this way but it is showing "org.hibernate.QueryParameterException: could not locate named parameter [param1]". 我试过这种方式,但它显示“org.hibernate.QueryParameterException:找不到命名参数[param1]”。 So can anyone please help me out how can I do this? 所以有人可以帮助我,我该怎么做?

You must place the HQL parameters using this notation: :paramName . 您必须使用以下表示法放置HQL参数:paramName Like this: 像这样:

select min([Cycle Time]) AS AvgMinCycleTime,
max([Cycle Time]) AS AvgMaxCycleTime,
AVG([Cycle Time]) AS AvgCycleTime
from EpicTable where EpicStartStatusName = :param1  
and EpicEndStatusName = :param2

And then: 接着:

Query que = session.createSQLQuery(queryString);
que.setParameter("param1",CycleStartStatus);
que.setParameter("param2",CycleEndStatus);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM