|
| 1 | += Queries |
| 2 | + |
| 3 | +== Keywords |
| 4 | + |
| 5 | +It is possible to use **most of the standard query keywords** for repositories defined in Spring Data JPA: https://docs.spring.io/spring-data/jpa/reference/repositories/query-keywords-reference.html[Spring Data JPA - Repository query keywords]. |
| 6 | + |
| 7 | +Here are a few examples: |
| 8 | + |
| 9 | +[source,java] |
| 10 | +---- |
| 11 | +@Repository |
| 12 | +public interface UserRepository extends EclipseStoreRepository<User, Long> |
| 13 | +{ |
| 14 | + List<User> findByFirstName(String firstName, String lastName); |
| 15 | + List<User> findByFirstNameAndLastName(String firstName, String lastName); |
| 16 | + List<User> findByDateOfBirthBefore(LocalDate date); |
| 17 | + List<User> findByAgeIn(List<Integer> ages); |
| 18 | + List<User> findByIsActiveFalse(); |
| 19 | +} |
| 20 | +---- |
| 21 | + |
| 22 | +More examples are in the https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/query/by/string/UserRepository.java[test-cases]. |
| 23 | + |
| 24 | +== Query by Example |
| 25 | + |
| 26 | +Developers can also use https://docs.spring.io/spring-data/jpa/reference/repositories/query-by-example.html[Query by Example] if preferred. |
| 27 | + |
| 28 | +An example: |
| 29 | + |
| 30 | +[source,java] |
| 31 | +---- |
| 32 | +public List<User> findAllUsersNamedMick() |
| 33 | +{ |
| 34 | + final User probe = new User(1, "Mick", BigDecimal.TEN); |
| 35 | + return userRepository.findAll(Example.of(probe)); |
| 36 | +} |
| 37 | +---- |
| 38 | + |
| 39 | +More examples are in the https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/query/by/example/QueryByExampleTest.java[test-cases]. |
| 40 | + |
| 41 | +== @Query annotation |
| 42 | + |
| 43 | +The support for a ``@Query``-Annotation is currently quite limited, but useful nonetheless. |
| 44 | + |
| 45 | +To keep parse and execute SQL-Queries we use the https://github.com/npgall/cqengine[cqengine] by https://github.com/npgall[Niall Gallagher]. |
| 46 | +It offers rudimentary support of some SQL-Queries, but not all. |
| 47 | + |
| 48 | +[NOTE] |
| 49 | +==== |
| 50 | +https://github.com/npgall/cqengine[cqengine] parses the SQL String as a SQLite-SQL-String and is therefore different from the https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html#jpa.query-methods.at-query[HQL or JPQL] of Spring Data JPA. |
| 51 | +==== |
| 52 | + |
| 53 | +Here are some working examples: |
| 54 | + |
| 55 | +[source,java] |
| 56 | +---- |
| 57 | +public interface MyEntityRepository extends ListCrudRepository<MyEntity, Long> |
| 58 | +{ |
| 59 | + @Query("SELECT * FROM MyEntity WHERE name = '?1'") |
| 60 | + List<MyEntity> findByName(String name); |
| 61 | +
|
| 62 | + @Query("SELECT * FROM MyEntity WHERE (name = '?1' AND age > ?2)") |
| 63 | + List<MyEntity> findByNameAndAgeGreaterThan(String name, int age); |
| 64 | +
|
| 65 | + @Query("SELECT * FROM MyEntity WHERE 'name' LIKE '%?1%'") |
| 66 | + List<MyEntity> findByNameContaining(String keyword); |
| 67 | +
|
| 68 | + @Query("SELECT * FROM MyEntity WHERE otherEntity IS NOT NULL") |
| 69 | + List<MyEntity> findWhereOtherEntityIsNotNull(); |
| 70 | +} |
| 71 | +---- |
| 72 | + |
| 73 | +More examples are in the https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/query/hsql/MyEntityRepository.java[test-cases]. |
0 commit comments