Class JPA

java.lang.Object
org.omnifaces.persistence.JPA

@Typed public final class JPA extends Object
JPA utilities.
  • Field Details

  • Method Details

    • 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 in persistence.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 as Optional.
      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 as Optional.
      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 - When T is of wrong type.
    • getSingleResultOrNull

      public static <T> T getSingleResultOrNull(TypedQuery<T> typedQuery)
      Returns single result of given typed query, or null 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, or null 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 - When T is of wrong type.
    • getOptionalFirstResult

      public static <T> Optional<T> getOptionalFirstResult(TypedQuery<T> typedQuery)
      Returns first result of given typed query as Optional. The difference with getOptionalSingleResult(TypedQuery) is that it doesn't throw NonUniqueResultException 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 as Optional. The difference with getOptionalSingleResult(Query) is that it doesn't throw NonUniqueResultException 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 - When T is of wrong type.
    • getFirstResultOrNull

      public static <T> T getFirstResultOrNull(TypedQuery<T> typedQuery)
      Returns first result of given typed query, or null if there is none. The difference with getSingleResultOrNull(TypedQuery) is that it doesn't throw NonUniqueResultException 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, or null if there is none. The difference with getSingleResultOrNull(Query) is that it doesn't throw NonUniqueResultException 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 - When T 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.
    • getCurrentBaseEntityService

      public static BaseEntityService<?,?> getCurrentBaseEntityService()
      Returns the currently active BaseEntityService from the SessionContext.
      Returns:
      The currently active BaseEntityService from the SessionContext.
      Throws:
      IllegalStateException - if there is none, which can happen if this method is called outside EJB context, or when currently invoked EJB service is not an instance of BaseEntityService.
    • 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 is Enumerated by EnumType.ORDINAL.
      Parameters:
      path - Path of interest.
      Returns:
      Whether given path is Enumerated by EnumType.ORDINAL.