如何使用 grails createCriteria 在休眠条件中编写嵌套的 AND 和 OR 块
我尝试编写以下 SQL 语句作为标准:
SELECT * FROM foo WHERE (a=1 AND b=1) OR (a=2 AND b=2) OR (a=3 AND b=4)
def textOrderCriteria = TextOrder.createCriteria()
textOrders = textOrderCriteria.list(max: 20, offset: 0) {
or {
and {
eq('a',1)
eq('b',1)
}
and {
eq('a',2)
eq('b',2)
}
and {
eq('a',3)
eq('b',4)
}
}
}
但这会破坏 TotalCount 并且结果不正确,因为每个 OR 不允许有多个 AND!
结果是(对于我的数据):
textOrders.size() == 6
textOrders.totalCount == 2
结果应该是(对于我的数据):
textOrders.size() == textOrders.totalCount
有解决这个问题或解决问题的想法吗?结果的大小怎么可能比totalCount还大呢? 我为此添加了一个 jira 问题: http://jira.grails.org/browse/GRAILS-7783
I tried to write the following SQL-Statement as a criteria:
SELECT * FROM foo WHERE (a=1 AND b=1) OR (a=2 AND b=2) OR (a=3 AND b=4)
def textOrderCriteria = TextOrder.createCriteria()
textOrders = textOrderCriteria.list(max: 20, offset: 0) {
or {
and {
eq('a',1)
eq('b',1)
}
and {
eq('a',2)
eq('b',2)
}
and {
eq('a',3)
eq('b',4)
}
}
}
but that breaks totalCount and the result is not corect because more than one AND per OR is not allowed!
The result is (for my data):
textOrders.size() == 6
textOrders.totalCount == 2
The result should be (for my data):
textOrders.size() == textOrders.totalCount
Any ideas to fix this or to work around? How could it be that the size of the result is bigger than totalCount?
I added a jira issue for this: http://jira.grails.org/browse/GRAILS-7783
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我无法将其作为评论发布(尚不允许!),但您可以尝试
在 config.groovy 的数据源块中设置标志,然后发布或检查正在生成的 SQL hibernate?这可能会让您了解正在发生的事情。
谢谢,
吉姆。
Sorry I can't post this as a comment (not allowed yet!) but you could try setting the
flag in the datasource block of your config.groovy and posting or checking the SQL hibernate is generating? This might give you a clue as to what's going on.
Thanks,
Jim.