【问题标题】:Exception in HQL query QuerySyntaxException unexpected token: >=HQL 查询中的异常 QuerySyntaxException 意外令牌:>=
【发布时间】:2015-12-03 05:37:51
【问题描述】:

我有以下在 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);

处于休眠状态

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个参数。

【问题讨论】:

  • 请不要无缘无故否决这个问题。

标签: java mysql sql hibernate hql


【解决方案1】:

在 where 子句中支持大小写,但在 HB3 中的 select 子句中不支持。 (根据 Hibernate 文档)

【讨论】:

  • 谢谢,有什么替代方案?
  • 我使用的是 Hibernate 4.3
【解决方案2】:

您可以使用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 ) ");

【讨论】:

  • 感谢一百万,太完美了。
  • @MehmoodMemon 我的荣幸 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2019-05-07
  • 1970-01-01
  • 2021-09-05
相关资源
最近更新 更多