有没有办法可以获得带有投影的不同Order对象列表(基于customerName
)(仅限选定字段)?
假设只有id不同,我想获取具有唯一customerName
的订单。是否可以使用预测或任何其他方式?
我的代码是:
def criteria = Order.createCriteria()
def orders = criteria.list() {
and {
eq("showAddress", true)
like("customerName", "%abcdPqrs%")
}
projections {
distinct("customerName")
property("deliveryAddress")
property("billingAddress")
property("")
}
}
return orders
上面的代码从Order获取重复的(customerName
)记录,我该如何解决?
答案 0 :(得分:1)
如果您将看到GORM生成的SQL查询,您会发现distinct将应用于完整行而不是customerName。您可以通过
启用日志logSql = true
在datasource.groovy。
你可以试试这个
def criteria = Order.createCriteria()
def orders = criteria.list() {
and {
eq("showAddress", true)
like("customerName", "%abcdPqrs%")
}
projections {
groupProperty("customerName")
property("deliveryAddress")
property("billingAddress")
property("")
}
}