Package com.broadleafcommerce.common.jpa
Class JpaEntityUtils
java.lang.Object
com.broadleafcommerce.common.jpa.JpaEntityUtils
Utility class providing some common functions related to working with the JPA API and Broadleaf
microservice entities.
- Author:
- Jeff Fischer
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidapplySort(jakarta.persistence.criteria.CriteriaQuery<T> query, jakarta.persistence.EntityManager em, List<jakarta.persistence.criteria.Order> orders, boolean isPaged) Apply any and all defined sort ordering to the JPA criteria query.static <T> voidapplySort(jakarta.persistence.criteria.CriteriaQuery<T> query, jakarta.persistence.EntityManager em, org.springframework.data.domain.Sort sort) Apply any and all defined sorts in theSortparam to the JPA criteria query.static Objectstatic FieldfindIdField(Class<?> entityClass) Find the field marked with the Spring or JPA @Id annotation in the entity class.static jakarta.persistence.criteria.PathgetPathForProperty(jakarta.persistence.criteria.Root<?> root, String property) Get a fully qualifiedPathbased on a period-delimited property path, possibly crossing multiple levels of "ToOne" associations.static <T> jakarta.persistence.criteria.Path<T>getPathForProperty(jakarta.persistence.criteria.Root<?> root, String property, Class<T> pathType) Get a fully qualifiedPathbased on a period-delimited property path, possibly crossing multiple levels of "ToOne" associations.
-
Method Details
-
getPathForProperty
public static <T> jakarta.persistence.criteria.Path<T> getPathForProperty(jakarta.persistence.criteria.Root<?> root, String property, Class<T> pathType) Get a fully qualifiedPathbased on a period-delimited property path, possibly crossing multiple levels of "ToOne" associations.- Type Parameters:
T- The generic type for the response path.- Parameters:
root- TheRootof the JPA criteria query.property- The period delimited property path.pathType- The type of the member identified by the property.- Returns:
- The Path identified by the property.
-
getPathForProperty
public static jakarta.persistence.criteria.Path getPathForProperty(jakarta.persistence.criteria.Root<?> root, String property) Get a fully qualifiedPathbased on a period-delimited property path, possibly crossing multiple levels of "ToOne" associations. The resulting path is not generically type specific.- Parameters:
root- TheRootof the JPA criteria query.property- The period delimited property path.- Returns:
- The Path identified by the property.
-
applySort
public static <T> void applySort(jakarta.persistence.criteria.CriteriaQuery<T> query, jakarta.persistence.EntityManager em, org.springframework.data.domain.Sort sort) Apply any and all defined sorts in theSortparam to the JPA criteria query. Furthermore, will add a final sort column for the id field of the entity (if applicable) to avoid indeterminate sorting results during paging if the requested sort fields don't already contain unique values in the result set.- Type Parameters:
T- The primary entity type dealt with in the query.- Parameters:
query- The JPA criteria query.em- The entity manager controlling the management of the query.sort- The Spring sort definition.
-
applySort
public static <T> void applySort(jakarta.persistence.criteria.CriteriaQuery<T> query, jakarta.persistence.EntityManager em, List<jakarta.persistence.criteria.Order> orders, boolean isPaged) Apply any and all defined sort ordering to the JPA criteria query. Furthermore, if paging is active, will add a final sort column for the id field of the entity (if applicable) to avoid indeterminate sorting results during paging if the requested sort fields don't already contain unique values in the result set.- Type Parameters:
T- The primary entity type dealt with in the query.- Parameters:
query- The JPA criteria query.em- The entity manager controlling the management of the query.orders- The list of JPA orders to sort the query by.isPaged- Whether or not the query is going to return paged results.
-
findIdField
Find the field marked with the Spring or JPA @Id annotation in the entity class. This process will search superclasses, should an id field not be found on the provided class. Will return null if no id annotated field is found.- Parameters:
entityClass- The class to check for the id annotated field- Returns:
- the id field or null if none found
- Throws:
IllegalArgumentException- if unable to find an id field
-
findId
-