使用 executeQuery() 方法从 Grails 中的数据库获取数据时遇到“org.hibernate.hql.PARSER”错误。请告诉我我做错了什么?

Facing `org.hibernate.hql.PARSER` Error while fetching data from DB in Grails with executeQuery() method. Please tell me what am I doing wrong?

我正在尝试基于日期时间获取数据,但我遇到了一些问题我的查询在 MySql 中有效,但在 Grails 中它给出了以下错误:

2015-09-11 11:59:00,697 ERROR org.hibernate.hql.PARSER:56 line 1:182: unexpected token: airDuration 2015-09-11 11:59:00,709 ERROR grails.app.controllers.com.my.test.rest.ipg.ChannelController:200 Invalid device parameter request : org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: airDuration near line 1, column 182 [ from com.my.test.ipgData.ChannelSchedule where channel = 77 and ((airDate between '2015-09-29 05:30:00' and '2015-09-29 20:30:00') or ((airDate + INTERVAL airDuration MINUTE) between '2015-09-29 05:30:00' and '2015-09-29 20:30:00')) order by airDate asc ]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: airDuration near line 1, column 182 [ from com.my.test.ipgData.ChannelSchedule where channel = 77 and ((airDate between '2015-09-29 05:30:00' and '2015-09-29 20:30:00') or ((airDate + INTERVAL airDuration MINUTE) between '2015-09-29 05:30:00' and '2015-09-29 20:30:00')) order by airDate asc ]


以下是我的代码:

ArrayList<ChannelSchedule> channelSchedule;

Date currentDateAndTime = new Date();
String scheduleQuery =  " from " +
                        "     ChannelSchedule " +
                        " where " +
                        "     channel = 77 and ((airDate between '2015-09-29 05:30:00' and '2015-09-29 20:30:00') or ((airDate + INTERVAL airDuration MINUTE) between '2015-09-29 05:30:00' and '2015-09-29 20:30:00')) " +
                        " order by airDate asc " ;

channelSchedule = ChannelSchedule.executeQuery(scheduleQuery);

频道时间表Class:

class ChannelSchedule {

    Date airDate;
    String airTime;
    int airDuration;
}

可以定义派生属性(与java中的@Formula相同)

class ChannelSchedule {

    Date airDate;
    String airTime;
    int airDuration;
    static mapping = {
         endTime formula: 'airDate + INTERVAL airDuration MINUTE'
    }
}

并在查询中使用 endTime

查看更多here

如果您稍微更改逻辑,就不必依赖 SQL 间隔。你绝对应该使用真实日期而不是字符串

ArrayList<ChannelSchedule> channelSchedule;

Date start
Date end = //Wherever this comes from
use( TimeCategory ) {
    start = /* Wherever the start comes from */ - 5.minutes
}


String scheduleQuery =  " from " +
                        "     ChannelSchedule " +
                        " where " +
                        "     channel = 77 and airDate between :start and :end " +
                        " order by airDate asc " ;

channelSchedule = ChannelSchedule.executeQuery(scheduleQuery, [start: start, end: end]);