2015-12-03 32 views
-1

我有以下查询,在MySQL中完美工作。但是当我在HQL中转换相同的查询时,它显示异常。HQL查询异常QuerySyntaxException异常令牌:> =

在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); 

在休眠

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和constructedDate是2个参数。

+0

请不要投下来的问题没有任何理由。 –

回答

2

的情况是在where子句中的支持,但不是在HB3选择子句。 (按照Hibernate文档)

+0

谢谢,有什么选择? –

+0

我正在使用休眠4.3 –

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) "); 
+0

感谢百万,这是完美的。 –

+0

@MehmoodMemon我的荣幸:) –