【问题标题】:HQL Join in GrailsHQL加入Grails
【发布时间】:2013-06-28 18:33:46
【问题描述】:

我正在尝试执行查询以收集特定数据,但在查询的 on 部分遇到问题。首先这是我的课:

    class TimeSlot {

    String timeslot_id
    String time_chunk_id
    String uid
    String exam_id
    String start_time
    String is_special_arrangement

    static mapping = {
        table 'timeslot'
        id name: "timeslot_id", column: "timeslot_id"
        version false
        }
    }

这是我正在尝试的查询:

TimeSlot.executeQuery("Select t.time_chunk_id, t.uid, t.start_time, t.timeslot_id, t.is_special_arrangement, e.length from TimeSlot t inner join Exams e on t.exam_id = e.exam_id where t.exam_id = ? and t.time_chunk_id = ?", [testArray[i], timeChunkArray[x]])

它在on 部分引发错误,因为它需要一个子句,但我需要数据专门与两个表的exam.id 比较有关。是否有其他方法或不同的方法来设置查询,使其能够像在任何 SQL 编辑器中一样工作?

【问题讨论】:

  • 在查询中使用with 而不是on。 HQL 不支持on 关键字。它的等价物是with
  • 这消除了错误,但现在它说 in can't resolve the symbol t at t.exam_id = e.exam_id.关于为什么会发生这种情况的任何建议?
  • 使用from TimeSlot as T, Exams as e where t.exam_id = e.exam_id
  • 我试图不将其拆分为两个不同的查询,因为它不会产生与我当前尝试使用的特定查询相关的相同结果。
  • 如果您还不知道,可以将域对象映射到exam_id 列。就像exams column: exam_idTimeSlot 里面一样,考试是Exams

标签: grails hql


【解决方案1】:

如果你改变域类并添加一对多关系会更容易

class TimeSlot {
    static hasMany = [examinations:Exams]

那么HQL可以

select ... from TimeSlot t join t.examinations e

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多