Spring Data JPA - Reference Documentation Memorandum

meleto 2019-07-25

此文章为Spring Data JPA - Reference Documentation(2.1.9.RELEASE)的备忘录。

Reference

Repository Query Keyword

Working with Spring Data Repositories

Core concepts

Repository

Combine method using find, count, get, delete, remove, read, distinct, OrderBy*Asc, and, lessThan, IgnoreCase, AllIgnoreCase.

Extended:
  1. CrudRepository: some simple method about CRUD, i guess.
  2. PagingAndSortingRepository:

Additional parameter candidates: Sort, Pageable(usage: PageRequest.of(1, 20)), Page can be returned.

Query method

package-info.java

4.3 Defining Repository Interface

Repository -> CrudRepository -> PagingAndSortingRepository -> JpaRepository(MangoDbRepository)

PageRequest.of(1, 20)
  1. By extending annotation before.
  2. By annotating with @RepositoryDefinition

Selectively expose crud method

@NoRepositoryBean
interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> {

  Optional<T> findById(ID id);

  <S extends T> S save(S entity);
}

interface UserRepository extends MyBaseRepository<User, Long> {
  User findByEmailAddress(EmailAddress emailAddress);
}
Make sure that you add @NoRepositoryBean to all repositories for which should not create an instance at runtime.

Null handling for repository method

  1. Add @NonNullApi on the package-info.java to make non-null constrain work.
  2. Using @NonNull and @Nullable to fine-tune.

Distinguishing to Spring Data Module

  1. Extending @JpaRepository, using Spring Data JPA module.
  2. Domain annotated with @Entity, using Spring Data JPA module, MongoDb if @Document.
  3. Annotated with @EnableJpaRepositories and @EnableMongoRepositories with attributes basePackages to define packages to be scanned as the corresponding repository.

4.4 Defining Query Method

@EnableJpaRepositories
basePackages; queryLookupStrategy; repositoryBaseClass can be used to customize the base repository.

4.4.1 Query Lookup Strategies

4.4.2 Query Creation

findTop10ByLastnameAndFirstnameAllIgnoreCaseOrderByGenderAcs
findByAddressZipCode: AddressZip.Code -> Address.ZipCode
findByAddress_ZipCode: Address.ZipCode

Slice<User> findByLastname(String lastname, Pageable pageable, Sort sort);
Stream<User>(Jave8 String<T>) can be returned.

Future<User>, CompletableFuture<User>, ListenableFuture<User> can be used with annotaion @Async

4.5 Creating Repository Instances

4.6 Custom Implementations for Spring Data Repository

interface HumanRepository {
  void someHumanMethod(User user);
}

// Impl postfix. In namespace configuration, postfix can be configured with attributes **repository-impl-postfix** in **repositories** tag
// If this class is annotated with **@Camponent("beanNameImpl")**, thing changed.
class HumanRepositoryImpl implements HumanRepository {

  public void someHumanMethod(User user) {
    // Your custom implementation
  }
}

interface ContactRepository {

  void someContactMethod(User user);

  User anotherContactMethod(User user);
}

class ContactRepositoryImpl implements ContactRepository {

  public void someContactMethod(User user) {
    // Your custom implementation
  }

  public User anotherContactMethod(User user) {
    // Your custom implementation
  }
}

// usage
interface UserRepository extends CrudRepository<User, Long>, HumanRepository, ContactRepository {

  // Declare query methods here
}
Custom implementations have a higher priority than base implementation. this feature let you can override base repository.

相关推荐

kyle00 / 0评论 2020-05-07
zhangdy0 / 0评论 2020-05-01
melonjj / 0评论 2020-02-18