【问题标题】:GORM Criteria Query: Finding Children with specific propertiesGORM Criteria Query:查找具有特定属性的子项
【发布时间】:2012-07-26 17:18:37
【问题描述】:

具有以下域模式:

class TransactionHeader {
  static hasMany = [details: TransactionDetail]
}

class TransactionDetail {
   static belongsTo = [header: TransactionHeader]
   Product product
}

我正在尝试编写一个条件查询,该查询将返回包含 TransactionDetails 和 2 个不同产品的所有 TransactionHeader 行。这就是我到目前为止所拥有的,但它并没有完全符合我的要求:

def list = TransactionHeader.withCriteria {
    details {
      and {
        eq("product", product1)
        eq("product", product2)
      }
    }
}

发生的情况是返回的行至少包含 1 个产品的 1 个详细信息。我需要有 2 个详细信息的行,每个都有一个产品。

【问题讨论】:

    标签: grails grails-orm criteria


    【解决方案1】:

    感觉你想从中删除细节,所以实际上

    def list = TransactionHeader.withCriteria {
        and {
           details {
              eq("product", product1)
           }
           details {
              eq("product", product2)
           }
        }
    }
    

    但不确定 hibernate / gorm 将如何处理这个问题。

    【讨论】:

      【解决方案2】:

      您是否尝试过在您的标准 dsl 中使用“in”语句?

      def list = TransactionHeader.withCriteria {
          and {
             details {
                'in'("product", [product1, product2])
             }
          }
      }
      

      【讨论】:

      • 在进程中作为 OR 这不是我想要的。我已经接受了正确答案。
      • 知道了,抱歉我没有意识到 and 约束
      猜你喜欢
      • 1970-01-01
      • 2015-06-08
      • 2014-07-28
      • 2013-01-23
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多