Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 3.18 KB

guide-devon4j-spring-repository.asciidoc

File metadata and controls

56 lines (41 loc) · 3.18 KB

Spring Data in devon4j-spring

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

<dependency>
  <groupId>com.devonfw.java.starters</groupId>
  <artifactId>devon4j-starter-spring-data-jpa</artifactId>
</dependency>

Example

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);
  }

}

Further examples

You can also read the JUnit test-case DefaultRepositoryTest that is testing an example FooRepository.

Auditing

In case you need auditing, you only need to extend DefaultRevisionedRepository instead of DefaultRepository. The auditing methods can be found in GenericRevisionedRepository.