-
Notifications
You must be signed in to change notification settings - Fork 88
guide devon4j spring repository
In devon4j-spring, spring-data-jpa is provided via devon4j-starter-spring-data-jpa extension, which provides advanced integration (esp. for QueryDSL).
Spring Data uses a fragment approach to implement custom functionality for repositories. For Spring applications, devon4j provides a solution that works without this fragment approach.
The repository must extend DefaultRepository, which uses GenericRepositoryImpl as implementation. The QueryUtil helper class provides methods to support pagination and query creation.
<dependency>
<groupId>com.devonfw.java.starters</groupId>
<artifactId>devon4j-starter-spring-data-jpa</artifactId>
</dependency>
The following example shows how to write such a repository. The example has the same functionality as the example in the Spring Data guide:
public interface ExampleRepository extends DefaultRepository<ExampleEntity> {
@Query("SELECT example FROM ExampleEntity example" //
+ " WHERE example.name = :name")
List<ExampleEntity> findByName(@Param("name") String name);
@Query("SELECT example FROM ExampleEntity example" //
+ " WHERE example.name = :name")
Page<ExampleEntity> findByNamePaginated(@Param("name") String name, Pageable pageable);
default Page<ExampleEntity> findByCriteria(ExampleSearchCriteriaTo criteria) {
ExampleEntity alias = newDslAlias();
JPAQuery<ExampleEntity> query = newDslQuery(alias);
String name = criteria.getName();
if ((name != null) && !name.isEmpty()) {
QueryUtil.get().whereString(query, $(alias.getName()), name, criteria.getNameOption());
}
return QueryUtil.get().findPaginated(criteria.getPageable(), query, false);
}
}
You can also read the JUnit test-case DefaultRepositoryTest that is testing an example FooRepository.
In case you need auditing, you only need to extend DefaultRevisionedRepository
instead of DefaultRepository
. The auditing methods can be found in GenericRevisionedRepository.
This documentation is licensed under the Creative Commons License (Attribution-NoDerivatives 4.0 International).