HQL查询中的异常QuerySyntaxException意外令牌:> =

时间:2015-12-03 05:37:51

标签: java mysql sql hibernate hql

我有以下在MySQL中完美运行的查询。但是当我在HQL中转换相同的查询时,它显示异常。

在MySQL中:

SET @periodDate='1988-07-01';
SET @completionDate='1957-06-30';
SELECT * FROM building_register_period WHERE  active = 1 
AND start_period_date <= @periodDate 
AND (CASE WHEN end_period_date IS NOT NULL THEN end_period_date >= @periodDate ELSE TRUE END)
AND (CASE WHEN constructed_start_date IS NOT NULL THEN constructed_start_date <= @completionDate ELSE TRUE END)
AND (CASE WHEN constructed_end_date IS NOT NULL THEN constructed_end_date >= @completionDate ELSE TRUE END);

在hibernate中

StringBuilder hql = new StringBuilder(" from BuildingRegisterPeriodModel brpm where brpm.active=true ");


            if(propertyValue1!=null && !propertyValue1.equals("") && propertyValue2!=null && !propertyValue2.equals("")) {
                hql.append("and brpm.startPeriodDate <= :periodDate and (case when brpm.endPeriodDate is not null then brpm.endPeriodDate >= :periodDate else true end) and (case when brpm.constructedStartDate is not null then brpm.constructedStartDate <= :constructedDate else true end) and (case brpm.constructedEndDate is not null then brpm.constructedEndDate >= :constructedDate else true end) ");
            }

这里,periodDate和constructDate是2个参数。

2 个答案:

答案 0 :(得分:2)

case子句支持case,但不支持HB3中的select子句。 (根据Hibernate文档)

答案 1 :(得分:0)

你可以使用OR这种情况下它会完美无缺。

 hql.append("and brpm.startPeriodDate <= :periodDate and (brpm.endPeriodDate is null or brpm.endPeriodDate >= :periodDate ) and (brpm.constructedStartDate is null or brpm.constructedStartDate <= :constructedDate ) and (brpm.constructedEndDate is null or brpm.constructedEndDate >= :constructedDate ) ");