【问题标题】:Grails Unidirectional Many to One and CriteriaGrails 单向多对一和标准
【发布时间】:2014-11-17 13:09:54
【问题描述】:

有什么方法可以查询单向多对一关系中一侧限制的多方(标准)列表

class Batch {

    String name
    Date date

}


class Record {
    String type
    Batch batch
}

class RecordDetails {
   String xx
   Record record
}

是否有条件查询所有批次记录,其中batch.date = xx,record.type = yy 和recordDetails.xx = zz

HQL 应该可以工作,但是除了查询之外,还有其他方法可以使用标准。

【问题讨论】:

  • 你大概可以用Where查询,见More Advanced Subqueries in GORM section
  • 我在 grails 2.2.4 上,不能使用它
  • 那么只有sqlRestriction 浮现在脑海中,但显然您必须编写原生 SQL。
  • 你能举个例子吗?
  • 当然,请看下面的答案..

标签: hibernate grails grails-orm


【解决方案1】:

对于 Where 查询和 Detached 条件,一个选项是使用 sqlRestriction,如下所示:

Batch.withCriteria {
    sqlRestriction """
        exists (
            select * from record r
            join record_details rd on rd.record_id = r.id 
            where 
                r.batch_id = {alias}.id and 
                r.type like ? and 
                rd.xx like ?
        )""", [ 'foo', 'bar' ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多