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
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final record
static final record
Partial query criteria, onlyCriteriaBuilder
andRoot
Used by enriched count and find query methods / lambdasstatic final record
static interface
Convenience interface for use withJPAFinder.CriteriaBuilderAndRoot
andJPAFinder.QueryCriteria
and is able to compose many enhanced query lambdas together -
Method Summary
Modifier and TypeMethodDescriptionlong
count()
count rowslong
count
(Consumer<JPAFinder.CountQueryCriteria<TT>> countQueryCriteria) count with enriched, composable criteriafindAll()
finds all entitiesfindAll
(Consumer<JPAFinder.QueryCriteria<TT>> queryCriteria) find all entities with enriched, composable criteriafindRange
(long min, long max) find entities given a specified rangefindRange
(long min, long max, Consumer<JPAFinder.QueryCriteria<TT>> queryCriteria) find entities with enriched, composable criteria given a specified range
-
Method Details
-
findAll
-
findAll
find all entities with enriched, composable criteriaExample:
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
find entities given a specified range- Parameters:
min
- minimum index, starting with zeromax
- maximum index- Returns:
- query
-
findRange
find entities with enriched, composable criteria given a specified range- Parameters:
min
- minimum index, starting with zeromax
- maximum indexqueryCriteria
-- Returns:
- query
-
count
long count()count rows- Returns:
- row count
-
count
count with enriched, composable criteria- Parameters:
countQueryCriteria
-- Returns:
- row count
-