2015-12-03 42 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我的榮幸:) –