简体   繁体   中英

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 que = session.createSQLQuery(queryString);

In this I want to set CycleStartStatus and CycleEndStatus using que.setParameter("param1",CycleStartStatus); and que.setParameter("param2",CycleEndStatus);

I tried this way but it is showing "org.hibernate.QueryParameterException: could not locate named parameter [param1]". So can anyone please help me out how can I do this?

You must place the HQL parameters using this notation: :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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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