Query by Boolean properties in spring-data-jpa without using method parameters

JavaSpring DataSpring Data-Jpa

Java Problem Overview


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

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

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

Java Solutions


Solution 1 - Java

The JPA repository section query creation has the following methods.

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

My guess would be to use

@Query
public Iterable<Entity> findByEnabledTrue();

Solution 2 - Java

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

public Iterable<Entity> findByEnabledTrue();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike MinickiView Question on Stackoverflow
Solution 1 - JavaorangegoatView Answer on Stackoverflow
Solution 2 - JavamegalucioView Answer on Stackoverflow