84

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();
Mike Minicki
  • 8,216
  • 11
  • 39
  • 43

2 Answers2

196

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();
hong4rc
  • 3,999
  • 4
  • 21
  • 40
orangegoat
  • 2,523
  • 1
  • 15
  • 17
  • Thanks, @orangegoat, but are you sure it works? Was it introduced in some later Spring Data version? I'm using 1.0.1 and I'm getting en exception: _Error creating bean with name 'entityRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: No property true found for type boolean_ – Mike Minicki Jul 11 '13 at 22:56
  • Well, it has been introduced in 1.1.0.RC1 version (2012-02-03): _"Support for True/False as query keywords (DATAJPA-132)"_. Marking as accepted even though I can't test if it works. – Mike Minicki Jul 11 '13 at 23:21
  • I think that works if you store it in database as true/false. If you store it as 'Y'/'N' this one will produce where x=1 and will fail when comparing string to int. I had to use it this way: findByX(boolean enabled) to get the right query. Not sure why translation doesn't happen as expected in first case.. – Vadim Kirilchuk Sep 18 '19 at 01:39
37

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

public Iterable<Entity> findByEnabledTrue();
megalucio
  • 5,051
  • 2
  • 22
  • 26
  • skip @Query? Not really, What about retrieving only specific fields? With your code you are retrieving all fields of the entity from the Database which may not be what you want, with that said, I add another version of your method: public Iterable findByEnabledIsTrue(); notice the Is or public Iterable findByEnabled(boolean done); then pass a boolean as argument – Melardev Jun 17 '19 at 15:28
  • 1
    You can indeed skip @Query annotation, as Spring will just infer that by convention based solely on the name of the method but of course if you want only specific fields you probably need to build a query, however I don't see what is the added value of that, and then as of your method findByEnabled that again is not the point of this question as clearly stated in the title of this question: 'Query by Boolean properties in spring-data-jpa without using method parameters' – megalucio Jun 17 '19 at 17:19
  • yes I know he asked for a solution without using @Query, but you said about skipping the Query annotation I just wanted to make sure people reading your answers they notice the consequence of that, query methods may be dangerous if you have an entity with many fields or/and fields that take a lot of memory(big strings for example). So no, you can not skip just like that – Melardev Jun 17 '19 at 17:42