Package org.omnifaces.persistence
Class JPA
- java.lang.Object
-
- org.omnifaces.persistence.JPA
-
-
Field Summary
Fields Modifier and Type Field Description static String
PROPERTY_VALIDATION_MODE
static String
QUERY_HINT_CACHE_RETRIEVE_MODE
static String
QUERY_HINT_CACHE_STORE_MODE
static String
QUERY_HINT_FETCH_GRAPH
static String
QUERY_HINT_LOAD_GRAPH
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Expression<String>
castAsString(CriteriaBuilder builder, Expression<?> expression)
Returns a new expression wherein given expression is cast as String.static Expression<String>
concat(CriteriaBuilder builder, Object... expressionsOrStrings)
Returns a SQL CONCAT(...) of given expressions or strings.static <T,I>
longcountForeignKeyReferences(EntityManager entityManager, Class<T> entityType, Class<I> identifierType, I id)
Returns count of all foreign key references to entity of given entity type with given ID of given identifier type.static <T> T
getFirstResultOrNull(Query query)
Returns first result of given query, ornull
if there is none.static <T> T
getFirstResultOrNull(TypedQuery<T> typedQuery)
Returns first result of given typed query, ornull
if there is none.static <T> Optional<T>
getOptionalFirstResult(Query query)
Returns first result of given query asOptional
.static <T> Optional<T>
getOptionalFirstResult(TypedQuery<T> typedQuery)
Returns first result of given typed query asOptional
.static <T> Optional<T>
getOptionalSingleResult(Query query)
Returns single result of given query asOptional
.static <T> Optional<T>
getOptionalSingleResult(TypedQuery<T> typedQuery)
Returns single result of given typed query asOptional
.static <K,T>
Map<K,T>getResultMap(TypedQuery<T> typedQuery, Function<? super T,? extends K> keyMapper)
Returns the result list of given typed query as a map mapped by the given key mapper.static <K,T,V>
Map<K,V>getResultMap(TypedQuery<T> typedQuery, Function<? super T,? extends K> keyMapper, Function<? super T,? extends V> valueMapper)
Returns the result list of given typed query as a map mapped by the given key and value mappers.static <T> T
getSingleResultOrNull(Query query)
Returns single result of given query, ornull
if there is none.static <T> T
getSingleResultOrNull(TypedQuery<T> typedQuery)
Returns single result of given typed query, ornull
if there is none.static ValidationMode
getValidationMode(EntityManager entityManager)
Returns the currently configured bean validation mode for given entity manager.static boolean
isEnumeratedByOrdinal(Path<?> path)
Returns whether given path isEnumerated
byEnumType.ORDINAL
.
-
-
-
Field Detail
-
QUERY_HINT_LOAD_GRAPH
public static final String QUERY_HINT_LOAD_GRAPH
- See Also:
- Constant Field Values
-
QUERY_HINT_FETCH_GRAPH
public static final String QUERY_HINT_FETCH_GRAPH
- See Also:
- Constant Field Values
-
QUERY_HINT_CACHE_STORE_MODE
public static final String QUERY_HINT_CACHE_STORE_MODE
- See Also:
- Constant Field Values
-
QUERY_HINT_CACHE_RETRIEVE_MODE
public static final String QUERY_HINT_CACHE_RETRIEVE_MODE
- See Also:
- Constant Field Values
-
PROPERTY_VALIDATION_MODE
public static final String PROPERTY_VALIDATION_MODE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getValidationMode
public static ValidationMode getValidationMode(EntityManager entityManager)
Returns the currently configured bean validation mode for given entity manager. This consults the "jakarta.persistence.validation.mode" property inpersistence.xml
.- Parameters:
entityManager
- The involved entity manager.- Returns:
- The currently configured bean validation mode.
-
getOptionalSingleResult
public static <T> Optional<T> getOptionalSingleResult(TypedQuery<T> typedQuery)
Returns single result of given typed query asOptional
.- Type Parameters:
T
- The generic result type.- Parameters:
typedQuery
- The involved typed query.- Returns:
- Single result of given typed query as
Optional
. - Throws:
NonUniqueResultException
- When there is no unique result.
-
getOptionalSingleResult
public static <T> Optional<T> getOptionalSingleResult(Query query)
Returns single result of given query asOptional
.- Type Parameters:
T
- The expected result type.- Parameters:
query
- The involved query.- Returns:
- Single result of given query as
Optional
. - Throws:
NonUniqueResultException
- When there is no unique result.ClassCastException
- WhenT
is of wrong type.
-
getSingleResultOrNull
public static <T> T getSingleResultOrNull(TypedQuery<T> typedQuery)
Returns single result of given typed query, ornull
if there is none.- Type Parameters:
T
- The generic result type.- Parameters:
typedQuery
- The involved typed query.- Returns:
- Single result of given typed query, or
null
if there is none. - Throws:
NonUniqueResultException
- When there is no unique result.
-
getSingleResultOrNull
public static <T> T getSingleResultOrNull(Query query)
Returns single result of given query, ornull
if there is none.- Type Parameters:
T
- The expected result type.- Parameters:
query
- The involved query.- Returns:
- Single result of given query, or
null
if there is none. - Throws:
NonUniqueResultException
- When there is no unique result.ClassCastException
- WhenT
is of wrong type.
-
getOptionalFirstResult
public static <T> Optional<T> getOptionalFirstResult(TypedQuery<T> typedQuery)
Returns first result of given typed query asOptional
. The difference withgetOptionalSingleResult(TypedQuery)
is that it doesn't throwNonUniqueResultException
when there are multiple matches.- Type Parameters:
T
- The generic result type.- Parameters:
typedQuery
- The involved typed query.- Returns:
- First result of given typed query as
Optional
.
-
getOptionalFirstResult
public static <T> Optional<T> getOptionalFirstResult(Query query)
Returns first result of given query asOptional
. The difference withgetOptionalSingleResult(Query)
is that it doesn't throwNonUniqueResultException
when there are multiple matches.- Type Parameters:
T
- The expected result type.- Parameters:
query
- The involved query.- Returns:
- First result of given query as
Optional
. - Throws:
ClassCastException
- WhenT
is of wrong type.
-
getFirstResultOrNull
public static <T> T getFirstResultOrNull(TypedQuery<T> typedQuery)
Returns first result of given typed query, ornull
if there is none. The difference withgetSingleResultOrNull(TypedQuery)
is that it doesn't throwNonUniqueResultException
when there are multiple matches.- Type Parameters:
T
- The generic result type.- Parameters:
typedQuery
- The involved typed query.- Returns:
- First result of given typed query, or
null
if there is none.
-
getFirstResultOrNull
public static <T> T getFirstResultOrNull(Query query)
Returns first result of given query, ornull
if there is none. The difference withgetSingleResultOrNull(Query)
is that it doesn't throwNonUniqueResultException
when there are multiple matches.- Type Parameters:
T
- The expected result type.- Parameters:
query
- The involved query.- Returns:
- First result of given query, or
null
if there is none. - Throws:
ClassCastException
- WhenT
is of wrong type.
-
getResultMap
public static <K,T> Map<K,T> getResultMap(TypedQuery<T> typedQuery, Function<? super T,? extends K> keyMapper)
Returns the result list of given typed query as a map mapped by the given key mapper.- Type Parameters:
K
- The generic map key type.T
- The generic result type, also map value type.- Parameters:
typedQuery
- The involved typed query.keyMapper
- The key mapper.- Returns:
- The result list of given typed query as a map mapped by the given key mapper.
-
getResultMap
public static <K,T,V> Map<K,V> getResultMap(TypedQuery<T> typedQuery, Function<? super T,? extends K> keyMapper, Function<? super T,? extends V> valueMapper)
Returns the result list of given typed query as a map mapped by the given key and value mappers.- Type Parameters:
K
- The generic map key type.T
- The generic result type.V
- The generic map value type.- Parameters:
typedQuery
- The involved typed query.keyMapper
- The key mapper.valueMapper
- The value mapper.- Returns:
- The result list of given typed query as a map mapped by the given key and value mappers.
-
countForeignKeyReferences
public static <T,I> long countForeignKeyReferences(EntityManager entityManager, Class<T> entityType, Class<I> identifierType, I id)
Returns count of all foreign key references to entity of given entity type with given ID of given identifier type. This is particularly useful in case you intend to check if the given entity is still referenced elsewhere in database.- Type Parameters:
T
- The generic result type.I
- The generic identifier type.- Parameters:
entityManager
- The involved entity manager.entityType
- Entity type.identifierType
- Identifier type.id
- Entity ID.- Returns:
- Count of all foreign key references to entity of given entity type with given ID of given identifier type.
-
concat
public static Expression<String> concat(CriteriaBuilder builder, Object... expressionsOrStrings)
Returns a SQL CONCAT(...) of given expressions or strings.- Parameters:
builder
- The involved criteria builder.expressionsOrStrings
- Expressions or Strings.- Returns:
- A SQL CONCAT(...) of given expressions or strings.
- Throws:
IllegalArgumentException
- When there are less than 2 expressions or strings. There's no point of concat then.
-
castAsString
public static Expression<String> castAsString(CriteriaBuilder builder, Expression<?> expression)
Returns a new expression wherein given expression is cast as String. This covers known problems with certain providers and/or databases.- Parameters:
builder
- The involved criteria builder.expression
- Expression to be cast as String.- Returns:
- A new expression wherein given expression is cast as String.
-
isEnumeratedByOrdinal
public static boolean isEnumeratedByOrdinal(Path<?> path)
Returns whether given path isEnumerated
byEnumType.ORDINAL
.- Parameters:
path
- Path of interest.- Returns:
- Whether given path is
Enumerated
byEnumType.ORDINAL
.
-
-