Interface JPAFinder<TT>

Type Parameters:
TT - Entity Type
All Known Subinterfaces:
JPAFinderHelper<TT>, JPANativeQuery<TT>
All Known Implementing Classes:
DaoHelper

public interface JPAFinder<TT>
Easily add a composable query enhancement criteria to findAll() and findRange(long, long) methods, as well as count() methods

Another differentiator is that this class doesn't require inheritance, although some use cases could inherit from InheritableDaoHelper class.

Simple Example:

@Stateless
public class ExampleDAO {
    @PersistenceContext(unitName = "demo-pu")
    EntityManager em;
    @Delegate
    JPAFinder<UserEntity> jpaFinder = new DaoHelper<>(() -> em, UserEntity.class);
}

Injected Example:

@Stateless
public class InjectedDAO {
    @Inject
    @Delegate
    JPAFinder<UserEntity> jpaFinder;
}

Injected Example with non-default EntityManager:

@Stateless
public class InjectedNonDefaultDAO {
    @Inject
    @Delegate
    @EntityManagerSelector(NonDefault.class)
    JPAFinder<UserEntity> jpaFinder;
}
Author:
lprimak
  • Method Details

    • findAll

      TypedQuery<TT> findAll()
      finds all entities
      Returns:
      query
    • findAll

      TypedQuery<TT> findAll(Consumer<JPAFinder.QueryCriteria<TT>> queryCriteria)
      find all entities with enriched, composable criteria

      Example:

      findAll(enhancement::accept)

      public record CountAndList(long count, List<UserEntity> list) { };
      public CountAndList countAndList(String userName) {
          // add "where fullName = 'userName'" clause
          QueryEnhancement<UserEntity> enhancement = (partial, criteria) -> criteria
                  .where(partial.builder().equal(partial.root()
                          .get(UserEntity_.fullName), userName));
      
          return new CountAndList(jpaFinder.count(enhancement::accept),
                  jpaFinder.findAll(enhancement::accept)
                  .setHint(QueryHints.BATCH_TYPE, BatchFetchType.IN)
                  .getResultList());
      }
      
      Parameters:
      queryCriteria -
      Returns:
      query
    • findRange

      TypedQuery<TT> findRange(long min, long max)
      find entities given a specified range
      Parameters:
      min - minimum index, starting with zero
      max - maximum index
      Returns:
      query
    • findRange

      TypedQuery<TT> findRange(long min, long max, Consumer<JPAFinder.QueryCriteria<TT>> queryCriteria)
      find entities with enriched, composable criteria given a specified range
      Parameters:
      min - minimum index, starting with zero
      max - maximum index
      queryCriteria -
      Returns:
      query
    • count

      long count()
      count rows
      Returns:
      row count
    • count

      long count(Consumer<JPAFinder.CountQueryCriteria<TT>> countQueryCriteria)
      count with enriched, composable criteria
      Parameters:
      countQueryCriteria -
      Returns:
      row count