Queries

Keywords

It is possible to use most of the standard query keywords for repositories defined in Spring Data JPA: Spring Data JPA - Repository query keywords.

Here are a few examples:

@Repository
public interface UserRepository extends EclipseStoreRepository<User, Long>
{
  List<User> findByFirstName(String firstName, String lastName);
  List<User> findByFirstNameAndLastName(String firstName, String lastName);
  List<User> findByDateOfBirthBefore(LocalDate date);
  List<User> findByAgeIn(List<Integer> ages);
  List<User> findByIsActiveFalse();
}

More examples are in the test-cases.

Query by Example

Developers can also use Query by Example if preferred.

An example:

public List<User> findAllUsersNamedMick()
{
  final User probe = new User(1, "Mick", BigDecimal.TEN);
  return userRepository.findAll(Example.of(probe));
}

More examples are in the test-cases.

@Query annotation

The support for a @Query-Annotation is currently quite limited, but useful nonetheless.

To keep parse and execute SQL-Queries we use the cqengine by Niall Gallagher. It offers rudimentary support of some SQL-Queries, but not all.

cqengine parses the SQL String as a SQLite-SQL-String and is therefore different from the HQL or JPQL of Spring Data JPA.

Here are some working examples:

public interface MyEntityRepository extends ListCrudRepository<MyEntity, Long>
{
  @Query("SELECT * FROM MyEntity WHERE name = '?1'")
  List<MyEntity> findByName(String name);

  @Query("SELECT * FROM MyEntity WHERE (name = '?1' AND age > ?2)")
  List<MyEntity> findByNameAndAgeGreaterThan(String name, int age);

  @Query("SELECT * FROM MyEntity WHERE 'name' LIKE '%?1%'")
  List<MyEntity> findByNameContaining(String keyword);

  @Query("SELECT * FROM MyEntity WHERE otherEntity IS NOT NULL")
  List<MyEntity> findWhereOtherEntityIsNotNull();
}

More examples are in the test-cases.