gpt4 book ai didi

java - 如何编写简单的 Hibernate Criteria 子查询

转载 作者:行者123 更新时间:2023-11-30 06:28:49 25 4
gpt4 key购买 nike

如何在 Java 中将此 SQL 查询编写为 Hibernate JPA Criteria(带有限制等)?

选择 q.*
FROM 队列 AS q
WHERE q.executed = false AND
q.queued_on = (SELECT min(queued_on) FROM 队列 WHERE item_id = q.item_id);

我只写了第一部分,如下所示:

getBaseCriteria()
.add(Restrictions.eq("executed", false))
// Missing Second Where Filter Here
.addOrder(Order.asc("queuedOn"))
.list();

最佳答案

尝试为子查询创建一个单独的条件实例,并简单地添加作为另一个限制,如下所示:

DetachedCriteria subCriteria = DetachedCriteria.forClass(Queue.class, "sub")
.add(Restrictions.eqProperty("sub.itemId","main.itemId"))
.setProjection(Projections.projectionList().add(Projections.min("sub.queuedOn")));

session.createCriteria(Queue.class, "main")
.add(Subqueries.propertyEq("main.queuedOn", subCriteria ));
.add(Restrictions.eq("main.executed", false));
.addOrder(Order.asc("main.queuedOn"))
.list();

关于java - 如何编写简单的 Hibernate Criteria 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46544267/

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