java 带条件的Spring JPQL和SIZE函数

83qze16e  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(82)

我在Sping Boot 应用程序的存储库中使用Hibernate和MySQL数据库实现了以下方法:

@Query("""
            SELECT tw FROM TW tw
            LEFT JOIN tw.docks d // where dock has flag isDeleted
            WHERE d.id = :dockId
            AND tw.status <> 'DELETED' AND SIZE(tw.docks) = 1
            """)

我想在SIZE函数中添加一些条件,以便只计算docksdock.isDeleted = false。是否可以或者必须编写一些原生查询?
编辑:添加ON d.isDeleted = false当然不是解决方案,因为它不会影响SIZE的功能
TimeWindow表和Dock表与第三表time_window_dock表连接

zpqajqem

zpqajqem1#

您可以使用聚合函数编写按查询分组:

SELECT tw 
FROM TW tw
LEFT JOIN tw.docks d ON d.isDeleted = false
WHERE d.id = :dockId
AND tw.status <> 'DELETED'
GROUP BY tw
HAVING COUNT(d.id) = 1

相关问题