Class SimpleJpaRepository<T,ID>

java.lang.Object
org.springframework.data.jpa.repository.support.SimpleJpaRepository<T,ID>
Type Parameters:
T - the type of the entity to handle
ID - the type of the entity's identifier
All Implemented Interfaces:
JpaRepository<T,ID>, JpaSpecificationExecutor<T>, JpaRepositoryImplementation<T,ID>, 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>
Direct Known Subclasses:
QuerydslJpaRepository

@Repository @Transactional(readOnly=true) public class SimpleJpaRepository<T,ID> extends Object implements JpaRepositoryImplementation<T,ID>
Default implementation of the CrudRepository interface. This will offer you a more sophisticated interface than the plain EntityManager .
Author:
Oliver Gierke, Eberhard Wolff, Thomas Darimont, Mark Paluch, Christoph Strobl, Stefan Fussenegger, Jens Schauder, David Madden, Moritz Becker, Sander Krabbenborg, Jesse Wouters, Greg Turnquist, Yanming Zhou, Ernst-Jan van der Laan, Diego Krupitza
  • Constructor Details

    • SimpleJpaRepository

      public SimpleJpaRepository(JpaEntityInformation<T,?> entityInformation, jakarta.persistence.EntityManager entityManager)
      Creates a new SimpleJpaRepository to manage objects of the given JpaEntityInformation.
      Parameters:
      entityInformation - must not be null.
      entityManager - must not be null.
    • SimpleJpaRepository

      public SimpleJpaRepository(Class<T> domainClass, jakarta.persistence.EntityManager entityManager)
      Creates a new SimpleJpaRepository to manage objects of the given domain type.
      Parameters:
      domainClass - must not be null.
      entityManager - must not be null.
  • Method Details

    • setRepositoryMethodMetadata

      public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata)
      Configures a custom CrudMethodMetadata to be used to detect LockModeTypes and query hints to be applied to queries.
      Specified by:
      setRepositoryMethodMetadata in interface JpaRepositoryImplementation<T,ID>
      Parameters:
      crudMethodMetadata -
    • setEscapeCharacter

      public void setEscapeCharacter(EscapeCharacter escapeCharacter)
      Description copied from interface: JpaRepositoryImplementation
      Configures the EscapeCharacter to be used with the repository.
      Specified by:
      setEscapeCharacter in interface JpaRepositoryImplementation<T,ID>
      Parameters:
      escapeCharacter - Must not be null.
    • getRepositoryMethodMetadata

      @Nullable protected CrudMethodMetadata getRepositoryMethodMetadata()
    • getDomainClass

      protected Class<T> getDomainClass()
    • deleteById

      @Transactional public void deleteById(ID id)
      Specified by:
      deleteById in interface org.springframework.data.repository.CrudRepository<T,ID>
    • delete

      @Transactional public void delete(T entity)
      Specified by:
      delete in interface org.springframework.data.repository.CrudRepository<T,ID>
    • deleteAllById

      @Transactional public void deleteAllById(Iterable<? extends ID> ids)
      Specified by:
      deleteAllById in interface org.springframework.data.repository.CrudRepository<T,ID>
    • deleteAllByIdInBatch

      @Transactional public void deleteAllByIdInBatch(Iterable<ID> ids)
      Description copied from interface: JpaRepository
      Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first level cache and the database out of sync. Consider flushing the EntityManager before calling this method.
      Specified by:
      deleteAllByIdInBatch in interface JpaRepository<T,ID>
      Parameters:
      ids - the ids of the entities to be deleted. Must not be null.
    • deleteAll

      @Transactional public void deleteAll(Iterable<? extends T> entities)
      Specified by:
      deleteAll in interface org.springframework.data.repository.CrudRepository<T,ID>
    • deleteAllInBatch

      @Transactional public void deleteAllInBatch(Iterable<T> entities)
      Description copied from interface: JpaRepository
      Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs first level cache and the database out of sync. Consider flushing the EntityManager before calling this method.

      It will also NOT honor cascade semantics of JPA, nor will it emit JPA lifecycle events.

      Specified by:
      deleteAllInBatch in interface JpaRepository<T,ID>
      Parameters:
      entities - entities to be deleted. Must not be null.
    • deleteAll

      @Transactional public void deleteAll()
      Specified by:
      deleteAll in interface org.springframework.data.repository.CrudRepository<T,ID>
    • deleteAllInBatch

      @Transactional public void deleteAllInBatch()
      Description copied from interface: JpaRepository
      Deletes all entities in a batch call.
      Specified by:
      deleteAllInBatch in interface JpaRepository<T,ID>
    • findById

      public Optional<T> findById(ID id)
      Specified by:
      findById in interface org.springframework.data.repository.CrudRepository<T,ID>
    • getOne

      @Deprecated public T getOne(ID id)
      Deprecated.
      Description copied from interface: JpaRepository
      Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw an EntityNotFoundException on first access. Some of them will reject invalid identifiers immediately.
      Specified by:
      getOne in interface JpaRepository<T,ID>
      Parameters:
      id - must not be null.
      Returns:
      a reference to the entity with the given identifier.
      See Also:
      • for details on when an exception is thrown.
    • getById

      @Deprecated public T getById(ID id)
      Deprecated.
      Description copied from interface: JpaRepository
      Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw an EntityNotFoundException on first access. Some of them will reject invalid identifiers immediately.
      Specified by:
      getById in interface JpaRepository<T,ID>
      Parameters:
      id - must not be null.
      Returns:
      a reference to the entity with the given identifier.
      See Also:
      • for details on when an exception is thrown.
    • getReferenceById

      public T getReferenceById(ID id)
      Description copied from interface: JpaRepository
      Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is implemented this is very likely to always return an instance and throw an EntityNotFoundException on first access. Some of them will reject invalid identifiers immediately.
      Specified by:
      getReferenceById in interface JpaRepository<T,ID>
      Parameters:
      id - must not be null.
      Returns:
      a reference to the entity with the given identifier.
      See Also:
      • for details on when an exception is thrown.
    • existsById

      public boolean existsById(ID id)
      Specified by:
      existsById in interface org.springframework.data.repository.CrudRepository<T,ID>
    • findAll

      public List<T> findAll()
      Specified by:
      findAll in interface org.springframework.data.repository.CrudRepository<T,ID>
      Specified by:
      findAll in interface org.springframework.data.repository.ListCrudRepository<T,ID>
    • findAllById

      public List<T> findAllById(Iterable<ID> ids)
      Specified by:
      findAllById in interface org.springframework.data.repository.CrudRepository<T,ID>
      Specified by:
      findAllById in interface org.springframework.data.repository.ListCrudRepository<T,ID>
    • findAll

      public List<T> findAll(org.springframework.data.domain.Sort sort)
      Specified by:
      findAll in interface org.springframework.data.repository.ListPagingAndSortingRepository<T,ID>
      Specified by:
      findAll in interface org.springframework.data.repository.PagingAndSortingRepository<T,ID>
    • findAll

      public org.springframework.data.domain.Page<T> findAll(org.springframework.data.domain.Pageable pageable)
      Specified by:
      findAll in interface org.springframework.data.repository.PagingAndSortingRepository<T,ID>
    • findOne

      public Optional<T> findOne(@Nullable Specification<T> spec)
      Description copied from interface: JpaSpecificationExecutor
      Returns a single entity matching the given Specification or Optional.empty() if none found.
      Specified by:
      findOne in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - can be null.
      Returns:
      never null.
    • findAll

      public List<T> findAll(@Nullable Specification<T> spec)
      Description copied from interface: JpaSpecificationExecutor
      Returns all entities matching the given Specification.
      Specified by:
      findAll in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - can be null.
      Returns:
      never null.
    • findAll

      public org.springframework.data.domain.Page<T> findAll(@Nullable Specification<T> spec, org.springframework.data.domain.Pageable pageable)
      Description copied from interface: JpaSpecificationExecutor
      Returns a Page of entities matching the given Specification.
      Specified by:
      findAll in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - can be null.
      pageable - must not be null.
      Returns:
      never null.
    • findAll

      public List<T> findAll(@Nullable Specification<T> spec, org.springframework.data.domain.Sort sort)
      Description copied from interface: JpaSpecificationExecutor
      Returns all entities matching the given Specification and Sort.
      Specified by:
      findAll in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - can be null.
      sort - must not be null.
      Returns:
      never null.
    • findOne

      public <S extends T> Optional<S> findOne(org.springframework.data.domain.Example<S> example)
      Specified by:
      findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • count

      public <S extends T> long count(org.springframework.data.domain.Example<S> example)
      Specified by:
      count in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • exists

      public <S extends T> boolean exists(org.springframework.data.domain.Example<S> example)
      Specified by:
      exists in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • exists

      public boolean exists(Specification<T> spec)
      Description copied from interface: JpaSpecificationExecutor
      Checks whether the data store contains elements that match the given Specification.
      Specified by:
      exists in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - the Specification to use for the existence check. Must not be null.
      Returns:
      true if the data store contains elements that match the given Specification otherwise false.
    • delete

      public long delete(Specification<T> spec)
      Description copied from interface: JpaSpecificationExecutor
      Deletes by the Specification and returns the number of rows deleted.

      This method uses Criteria API bulk delete that maps directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.

      Please note that CriteriaQuery in, Specification.toPredicate(Root, CriteriaQuery, CriteriaBuilder) will be null because CriteriaBuilder.createCriteriaDelete(Class) does not implement CriteriaQuery.

      Specified by:
      delete in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - the Specification to use for the existence check. Must not be null.
      Returns:
      the number of entities deleted.
    • findAll

      public <S extends T> List<S> findAll(org.springframework.data.domain.Example<S> example)
      Specified by:
      findAll in interface JpaRepository<T,ID>
      Specified by:
      findAll in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • findAll

      public <S extends T> List<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Sort sort)
      Specified by:
      findAll in interface JpaRepository<T,ID>
      Specified by:
      findAll in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • findAll

      public <S extends T> org.springframework.data.domain.Page<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Pageable pageable)
      Specified by:
      findAll in interface org.springframework.data.repository.query.QueryByExampleExecutor<T>
    • 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>
    • findBy

      public <S extends T, R> R findBy(Specification<T> spec, Function<org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery<S>,R> queryFunction)
      Description copied from interface: JpaSpecificationExecutor
      Returns entities matching the given Specification applying the queryFunction that defines the query and its result type.
      Specified by:
      findBy in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - – must not be null.
      queryFunction - – the query function defining projection, sorting, and the result type
      Returns:
      all entities matching the given Example.
    • count

      public long count()
      Specified by:
      count in interface org.springframework.data.repository.CrudRepository<T,ID>
    • count

      public long count(@Nullable Specification<T> spec)
      Description copied from interface: JpaSpecificationExecutor
      Returns the number of instances that the given Specification will return.
      Specified by:
      count in interface JpaSpecificationExecutor<T>
      Parameters:
      spec - the Specification to count instances for. Can be null.
      Returns:
      the number of instances.
    • save

      @Transactional public <S extends T> S save(S entity)
      Specified by:
      save in interface org.springframework.data.repository.CrudRepository<T,ID>
    • saveAndFlush

      @Transactional public <S extends T> S saveAndFlush(S entity)
      Description copied from interface: JpaRepository
      Saves an entity and flushes changes instantly.
      Specified by:
      saveAndFlush in interface JpaRepository<T,ID>
      Parameters:
      entity - entity to be saved. Must not be null.
      Returns:
      the saved entity
    • saveAll

      @Transactional public <S extends T> List<S> saveAll(Iterable<S> entities)
      Specified by:
      saveAll in interface org.springframework.data.repository.CrudRepository<T,ID>
      Specified by:
      saveAll in interface org.springframework.data.repository.ListCrudRepository<T,ID>
    • saveAllAndFlush

      @Transactional public <S extends T> List<S> saveAllAndFlush(Iterable<S> entities)
      Description copied from interface: JpaRepository
      Saves all entities and flushes changes instantly.
      Specified by:
      saveAllAndFlush in interface JpaRepository<T,ID>
      Parameters:
      entities - entities to be saved. Must not be null.
      Returns:
      the saved entities
    • flush

      @Transactional public void flush()
      Description copied from interface: JpaRepository
      Flushes all pending changes to the database.
      Specified by:
      flush in interface JpaRepository<T,ID>
    • readPage

      @Deprecated protected org.springframework.data.domain.Page<T> readPage(jakarta.persistence.TypedQuery<T> query, org.springframework.data.domain.Pageable pageable, @Nullable Specification<T> spec)
      Reads the given TypedQuery into a Page applying the given Pageable and Specification.
      Parameters:
      query - must not be null.
      spec - can be null.
      pageable - must not be null.
    • readPage

      protected <S extends T> org.springframework.data.domain.Page<S> readPage(jakarta.persistence.TypedQuery<S> query, Class<S> domainClass, org.springframework.data.domain.Pageable pageable, @Nullable Specification<S> spec)
      Reads the given TypedQuery into a Page applying the given Pageable and Specification.
      Parameters:
      query - must not be null.
      domainClass - must not be null.
      spec - can be null.
      pageable - can be null.
    • getQuery

      protected jakarta.persistence.TypedQuery<T> getQuery(@Nullable Specification<T> spec, org.springframework.data.domain.Pageable pageable)
      Creates a new TypedQuery from the given Specification.
      Parameters:
      spec - can be null.
      pageable - must not be null.
    • getQuery

      protected <S extends T> jakarta.persistence.TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, org.springframework.data.domain.Pageable pageable)
      Creates a new TypedQuery from the given Specification.
      Parameters:
      spec - can be null.
      domainClass - must not be null.
      pageable - must not be null.
    • getQuery

      protected jakarta.persistence.TypedQuery<T> getQuery(@Nullable Specification<T> spec, org.springframework.data.domain.Sort sort)
      Creates a TypedQuery for the given Specification and Sort.
      Parameters:
      spec - can be null.
      sort - must not be null.
    • getQuery

      protected <S extends T> jakarta.persistence.TypedQuery<S> getQuery(@Nullable Specification<S> spec, Class<S> domainClass, org.springframework.data.domain.Sort sort)
      Creates a TypedQuery for the given Specification and Sort.
      Parameters:
      spec - can be null.
      domainClass - must not be null.
      sort - must not be null.
    • getCountQuery

      @Deprecated protected jakarta.persistence.TypedQuery<Long> getCountQuery(@Nullable Specification<T> spec)
      Deprecated.
      Creates a new count query for the given Specification.
      Parameters:
      spec - can be null.
    • getCountQuery

      protected <S extends T> jakarta.persistence.TypedQuery<Long> getCountQuery(@Nullable Specification<S> spec, Class<S> domainClass)
      Creates a new count query for the given Specification.
      Parameters:
      spec - can be null.
      domainClass - must not be null.
    • getQueryHints

      protected QueryHints getQueryHints()
      Returns QueryHints with the query hints based on the current CrudMethodMetadata and potential EntityGraph information.
    • getQueryHintsForCount

      protected QueryHints getQueryHintsForCount()
      Returns QueryHints with the query hints on the current CrudMethodMetadata for count queries.