Class InMemoryJpaRepository<T,ID extends Comparable<? super ID>>

java.lang.Object
com.reallifedeveloper.tools.test.database.inmemory.AbstractInMemoryCrudRepository<T,ID>
com.reallifedeveloper.tools.test.database.inmemory.InMemoryJpaRepository<T,ID>
Type Parameters:
T - the type of the entities handled by this repository
ID - the type of the entities' primary keys
All Implemented Interfaces:
org.springframework.data.jpa.repository.JpaRepository<T,ID>, org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>, org.springframework.data.repository.CrudRepository<T,ID>, org.springframework.data.repository.ListCrudRepository<T,ID>, org.springframework.data.repository.ListPagingAndSortingRepository<T,ID>, org.springframework.data.repository.PagingAndSortingRepository<T,ID>, org.springframework.data.repository.query.QueryByExampleExecutor<T>, org.springframework.data.repository.Repository<T,ID>

public class InMemoryJpaRepository<T,ID extends Comparable<? super ID>> extends AbstractInMemoryCrudRepository<T,ID> implements org.springframework.data.jpa.repository.JpaRepository<T,ID>, org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
An implementation of the Spring Data JPA JpaRepository interface that holds entities in a map. Useful for testing.
Author:
RealLifeDeveloper
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.springframework.data.jpa.repository.JpaSpecificationExecutor

    org.springframework.data.jpa.repository.JpaSpecificationExecutor.SpecificationFluentQuery<T>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new InMemoryJpaRepository with no primary key generator.
    Creates a new InMemoryJpaRepository with the provided primary key generator.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    count(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
     
    long
    count(org.springframework.data.jpa.domain.Specification<T> spec)
     
    long
    delete(org.springframework.data.jpa.domain.DeleteSpecification<T> spec)
     
    long
    delete(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
     
    void
    void
    void
    boolean
    exists(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
     
    boolean
    exists(org.springframework.data.jpa.domain.Specification<T> spec)
     
    findAll(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
     
    findAll(org.springframework.data.jpa.domain.Specification<T> spec)
     
    org.springframework.data.domain.Page<T>
    findAll(org.springframework.data.jpa.domain.Specification<T> spec, org.springframework.data.domain.Pageable pageable)
     
    findAll(org.springframework.data.jpa.domain.Specification<T> spec, org.springframework.data.domain.Sort sort)
     
    org.springframework.data.domain.Page<T>
    findAll(org.springframework.data.jpa.domain.Specification<T> spec, org.springframework.data.jpa.domain.Specification<T> countSpec, org.springframework.data.domain.Pageable pageable)
     
    <S extends T, R>
    R
    findBy(org.springframework.data.domain.Example<S> example, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>,R> queryFunction)
     
    <S extends T, R>
    R
    findBy(org.springframework.data.jpa.domain.PredicateSpecification<T> spec, Function<? super org.springframework.data.jpa.repository.JpaSpecificationExecutor.SpecificationFluentQuery<S>,R> queryFunction)
     
    <S extends T, R>
    R
    findBy(org.springframework.data.jpa.domain.Specification<T> spec, Function<? super org.springframework.data.jpa.repository.JpaSpecificationExecutor.SpecificationFluentQuery<S>,R> queryFunction)
     
    findOne(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
     
    findOne(org.springframework.data.jpa.domain.Specification<T> spec)
     
    void
    getById(ID id)
    protected Optional<Class<ID>>
    Override this in concrete subclass to give the ID class representing a composite primary key for an entity, if any.
    getOne(ID id)
     
    protected boolean
    Override this in a concrete subclass to decide if a given field is an ID field of an entity.
    protected boolean
    Override this in a concrete subclass to decide if a given method is a method giving the ID of an entity.
    Gives the PredicateSpecificationEvaluator used by this repository.
    <S extends T>
    List<S>
    <S extends T>
    S
    saveAndFlush(S entity)
    long
    update(org.springframework.data.jpa.domain.UpdateSpecification<T> spec)
     

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.springframework.data.repository.CrudRepository

    count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, save

    Methods inherited from interface org.springframework.data.jpa.repository.JpaRepository

    deleteInBatch, findAll, findAll

    Methods inherited from interface org.springframework.data.repository.ListCrudRepository

    findAll, findAllById, saveAll

    Methods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.repository.PagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor

    count, exists, findAll, findOne
  • Constructor Details

    • InMemoryJpaRepository

      public InMemoryJpaRepository()
      Creates a new InMemoryJpaRepository with no primary key generator. If an entity with a null primary key is saved, an exception is thrown.
    • InMemoryJpaRepository

      public InMemoryJpaRepository(PrimaryKeyGenerator<ID> primaryKeyGenerator)
      Creates a new InMemoryJpaRepository with the provided primary key generator. If an entity with a null primary key is saved, the generator is used to create a new primary key that is stored in the entity before saving.
      Parameters:
      primaryKeyGenerator - the primary key generator to use
  • Method Details

    • predicateSpecificationEvaluator

      public PredicateSpecificationEvaluator<T> predicateSpecificationEvaluator()
      Gives the PredicateSpecificationEvaluator used by this repository. This is useful for registering functions used by PredicateSpecifications, and could occasionally be useful to directly check if a predicate specification matches an entity.
      Returns:
      the PredicateSpecificationEvaluator used by this repository.
    • deleteAllInBatch

      public void deleteAllInBatch()
      Specified by:
      deleteAllInBatch in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • deleteAllInBatch

      public void deleteAllInBatch(Iterable<T> entities)
      Specified by:
      deleteAllInBatch in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • deleteAllByIdInBatch

      public void deleteAllByIdInBatch(Iterable<ID> ids)
      Specified by:
      deleteAllByIdInBatch in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • flush

      public void flush()
      Specified by:
      flush in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • saveAndFlush

      public <S extends T> S saveAndFlush(S entity)
      Specified by:
      saveAndFlush in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • saveAllAndFlush

      public <S extends T> List<S> saveAllAndFlush(Iterable<S> entities)
      Specified by:
      saveAllAndFlush in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • getById

      public T getById(ID id)
      Specified by:
      getById in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • getOne

      public T getOne(ID id)
      Specified by:
      getOne in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • isIdField

      protected boolean isIdField(Field field)
      Description copied from class: AbstractInMemoryCrudRepository
      Override this in a concrete subclass to decide if a given field is an ID field of an entity.
      Specified by:
      isIdField in class AbstractInMemoryCrudRepository<T,ID extends Comparable<? super ID>>
      Parameters:
      field - the field to examine
      Returns:
      true if field is an ID field, false otherwise
    • isIdMethod

      protected boolean isIdMethod(Method method)
      Description copied from class: AbstractInMemoryCrudRepository
      Override this in a concrete subclass to decide if a given method is a method giving the ID of an entity.
      Specified by:
      isIdMethod in class AbstractInMemoryCrudRepository<T,ID extends Comparable<? super ID>>
      Parameters:
      method - the method to examine
      Returns:
      true if method is an ID method, false otherwise
    • getCompositeIdClass

      protected Optional<Class<ID>> getCompositeIdClass(Object entity)
      Description copied from class: AbstractInMemoryCrudRepository
      Override this in concrete subclass to give the ID class representing a composite primary key for an entity, if any.
      Specified by:
      getCompositeIdClass in class AbstractInMemoryCrudRepository<T,ID extends Comparable<? super ID>>
      Parameters:
      entity - the entity to examine
      Returns:
      the ID class representing the composite primary key of entity, or an empty optional if there is no such class
    • findBy

      public <S extends T, R> R findBy(org.springframework.data.domain.Example<S> example, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>,R> queryFunction)
      Specified by:
      findBy in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • getReferenceById

      public T getReferenceById(ID id)
      Specified by:
      getReferenceById in interface org.springframework.data.jpa.repository.JpaRepository<T,ID extends Comparable<? super ID>>
    • count

      public long count(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
      Specified by:
      count in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • delete

      public long delete(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
      Specified by:
      delete in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • exists

      public boolean exists(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
      Specified by:
      exists in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findAll

      public List<T> findAll(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
      Specified by:
      findAll in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findBy

      public <S extends T, R> R findBy(org.springframework.data.jpa.domain.PredicateSpecification<T> spec, Function<? super org.springframework.data.jpa.repository.JpaSpecificationExecutor.SpecificationFluentQuery<S>,R> queryFunction)
      Specified by:
      findBy in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findOne

      public Optional<T> findOne(org.springframework.data.jpa.domain.PredicateSpecification<T> spec)
      Specified by:
      findOne in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • delete

      public long delete(org.springframework.data.jpa.domain.DeleteSpecification<T> spec)
      Specified by:
      delete in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • count

      public long count(org.springframework.data.jpa.domain.Specification<T> spec)
      Specified by:
      count in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • exists

      public boolean exists(org.springframework.data.jpa.domain.Specification<T> spec)
      Specified by:
      exists in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findAll

      public List<T> findAll(org.springframework.data.jpa.domain.Specification<T> spec)
      Specified by:
      findAll in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findAll

      public org.springframework.data.domain.Page<T> findAll(org.springframework.data.jpa.domain.Specification<T> spec, org.springframework.data.domain.Pageable pageable)
      Specified by:
      findAll in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findAll

      public List<T> findAll(org.springframework.data.jpa.domain.Specification<T> spec, org.springframework.data.domain.Sort sort)
      Specified by:
      findAll in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findAll

      public org.springframework.data.domain.Page<T> findAll(org.springframework.data.jpa.domain.Specification<T> spec, org.springframework.data.jpa.domain.Specification<T> countSpec, org.springframework.data.domain.Pageable pageable)
      Specified by:
      findAll in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findBy

      public <S extends T, R> R findBy(org.springframework.data.jpa.domain.Specification<T> spec, Function<? super org.springframework.data.jpa.repository.JpaSpecificationExecutor.SpecificationFluentQuery<S>,R> queryFunction)
      Specified by:
      findBy in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • findOne

      public Optional<T> findOne(org.springframework.data.jpa.domain.Specification<T> spec)
      Specified by:
      findOne in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>
    • update

      public long update(org.springframework.data.jpa.domain.UpdateSpecification<T> spec)
      Specified by:
      update in interface org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>