简体   繁体   English

JPA 查询:如何检查是 NULL 还是不是 NULL?

[英]JPA Query : How to check IS NULL or IS NOT NULL?

I have below parameter which is我有以下参数

Boolean isActive = true/false

I need to pass this parameter in JPA Query.我需要在 JPA 查询中传递这个参数。

true =  IS NOT NULL
false = IS NULL

@Query("select t from Test t WHERE t.active = ?1")
Optional<Test> findByIds(Boolean isActive);

How can I achieve this, rather than creating 2 separate queries for IS NULL & IS NOT NULL.我该如何实现这一点,而不是为 IS NULL & IS NOT NULL 创建 2 个单独的查询。

You can achieve that with the following way您可以通过以下方式实现

@Query("select t from Test t WHERE (?1 = true AND t.active IS NOT NULL) OR
                                   (?1 = false AND t.active IS NULL")
Optional<Test> findByIds(Boolean isActive);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM