gpt4 book ai didi

grails - 带有关联的Grails “max”子查询,仅获取hasMany的最新信息

转载 作者:行者123 更新时间:2023-12-02 14:08:59 24 4
gpt4 key购买 nike

简化的领域模型(Domain Model):
“Txn”(如在交易中)具有许多“TxnStatus”。 TxnStatus有一个dateTime

这是旧版映射,因此我无法更改数据库,即Txn上的映射:

  static mapping = { 
txnStatus column: 'MessageID', ignoreNotFound: true, fetch: 'join'
}

我需要基于许多动态建立的标准来获取Txns,当前使用GORM的“where”查询,它工作得很好。但是我还需要仅获取 最新 txnStatus。

尝试过:
def query = Txn.where {
txnStatus { dateTime == max(dateTime) }
}

给出: java.lang.ClassCastException: org.hibernate.criterion.DetachedCriteria cannot be cast to java.util.Date
还尝试了:
 def query = Txn.where {
txnStatus.dateTime == max(txnStatus.dateTime)
}

这使:
Compilation Error: ... 
Cannot use aggregate function max on expressions "txnStatus.dateTime"

在这个阶段,我正在考虑改用HQL ...任何帮助!

最佳答案

几天前有一个非常类似的问题。看来,将带有“max”子查询的where查询与==搭配使用效果不佳

OP能够使其与<一起使用,并以这种方式解决。查看有关查询位置的文档并没有帮助我弄清楚这一点。

这是一个非常疯狂的猜测-

    Txn.where {
txnStatus {
dateTime == property(dateTime).of { max(dateTime) }
}
}

关于grails - 带有关联的Grails “max”子查询,仅获取hasMany的最新信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10610338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com