5

For example, I have entity User with fields id, active and 10 more. How could I get all active Users? Easy:

public interface UserRepository extends JpaRepository<User, Integer> {
    List<User> findAllByActiveTrue();
}

How could I load list of id of active users? I need a @Query annotation to write JPQL by myself, like this:

public interface UserRepository extends JpaRepository<User, Integer> {
    @Query("select u.id from User u where u.active = true")
    List<Integer> fetchActiveUsersIds();
}

My question is: could I name somehow a method to avoid writing JQPL by myself, like in first case?

UPDATE

I need something like this:

public interface UserRepository extends JpaRepository<User, Integer> {
    List<Integer> findAll_Id_ByActiveTrue();
}
AlexB
  • 473
  • 2
  • 7
  • 16
  • The name of the method is given by the interface. If you change the name it will change nothing. – Roman C Jan 30 '16 at 13:32
  • @RomanC I mean the name, that generates JPA query, like in first code example – AlexB Jan 30 '16 at 13:36
  • http://stackoverflow.com/questions/30331767/spring-data-jpa-how-to-get-only-a-list-of-ids-from-findall-method – leeor Jan 30 '16 at 13:52
  • @leeor That's not about what I want. No, that question looks similar to mine, but solution is much more verbose than mine. – AlexB Jan 30 '16 at 14:08

1 Answers1

1

I think that is not possible. The main purpose of Repositoryis to manage persistence of Domain Classes. It doesn't manage the properties independently. In the Spring Data Repositories documentation you can find a list of the methods available for CrudRepository.

You can compose your query using property extensions but it always returns the domain class for non aggregated methods.

For the specific use case you mention you'll need to use the @Query annotation.

rvazquezglez
  • 2,284
  • 28
  • 40