【问题标题】:How to get distinct results using Grails GORM如何使用 Grails GORM 获得不同的结果
【发布时间】:2015-04-21 10:35:33
【问题描述】:

我在两个对象之间有一对多的关系。喜欢订单和商品

class Order {
   static hasMany = [items: ItemModel]
   ...//some properties
}

class ItemModel {
   // some properties
}

  class SearchController {
   def doSearch() {
      def criteriaOrder = Order.createCriteria();
      def order =  criteriaOrder.list(params) {
         and {
            items {
               like("partNumber", itemName)
            }
            or {
               if (customerName){
                  like('bclientName', customerName)
                  like('dclientName',customerName)
               }
            }
            if (fromDate && toDate) {
               between('orderDate', fromDate,toDate)
            }
         }
      }
     }
   }

当我要搜索时,它会给出重复的结果。我在列表对象上使用了 unique(),它给出了 unqiue 但分页不起作用。

【问题讨论】:

标签: grails grails-orm


【解决方案1】:

distinct projection 添加到您的查询中:

基于不同的解决方案:

def order =  criteriaOrder.list(params) {
     projections{ 
       distinct 'id'
       property 'bclientName'
       property 'dclientName'
       property 'orderDate'
       // add further props you need to show in the list
     }
     and {
        items {
           like("partNumber", itemName)
        }
  ....
}.collect{ 
  Order o = new Order( bclientName:it[ 1 ], dclientName:it[ 2 ], orderDate:it[ 3 ] ) 
  o.id = it[ 0 ]
  o
}

基于 groupBy 的解决方案:

def order =  criteriaOrder.list(params) {
     projections{ 
       groupProperty 'id'
       max 'orderDate'
     }
     and {
        items {
           like("partNumber", itemName)
        }
  ....
}

【讨论】:

  • 是的,它在结果中给出了 id,但我想获取订单对象
  • 您需要将完整的对象呈现为列表,还是只呈现几个字段?
  • 订单对象的完整列表
  • 根据预测它给出了所有 id 但我需要对象
  • 我仍然只得到订单的对象 ID,但我需要完整的对象
猜你喜欢
  • 2021-08-02
  • 2012-09-18
  • 1970-01-01
  • 2019-04-14
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
相关资源
最近更新 更多