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();
}
4
  • The name of the method is given by the interface. If you change the name it will change nothing. Commented Jan 30, 2016 at 13:32
  • @RomanC I mean the name, that generates JPA query, like in first code example Commented Jan 30, 2016 at 13:36
  • stackoverflow.com/questions/30331767/… Commented Jan 30, 2016 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. Commented Jan 30, 2016 at 14:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.