在不使用方法参数的情况下,通过spring-data-jpa中的布尔属性进行查询

时间:2021-08-22 20:06:01

Is it possible to query by Boolean properties in Spring Data JPA without using method parameters?

是否可以在不使用方法参数的情况下通过Spring Data JPA中的布尔属性进行查询?

Basically I would like this to work without using custom @Query annotation:

基本上我希望这可以在不使用自定义@Query注释的情况下工作:

@Query("SELECT c FROM Entity c WHERE c.enabled = true")
public Iterable<Entity> findAllEnabled();

2 个解决方案

#1


75  

The JPA repository section query creation has the following methods.

JPA存储库部分查询创建具有以下方法。

True    findByActiveTrue()  … where x.active = true
False   findByActiveFalse() … where x.active = false

My guess would be to use

我的猜测是使用

@Query
public Iterable<Entity> findByEnabledTrue();

#2


16  

The @Query anotation can even be skipped. So it should just work just like this:

甚至可以跳过@Query anotation。所以它应该像这样工作:

public Iterable<Entity> findByEnabledTrue();

#1


75  

The JPA repository section query creation has the following methods.

JPA存储库部分查询创建具有以下方法。

True    findByActiveTrue()  … where x.active = true
False   findByActiveFalse() … where x.active = false

My guess would be to use

我的猜测是使用

@Query
public Iterable<Entity> findByEnabledTrue();

#2


16  

The @Query anotation can even be skipped. So it should just work just like this:

甚至可以跳过@Query anotation。所以它应该像这样工作:

public Iterable<Entity> findByEnabledTrue();