Class EntityUtils


  • public final class EntityUtils
    extends Object
    Dynamically perform operations related to repository domain classes.
    Author:
    Jeff Fischer
    • Method Detail

      • findIdField

        public static Field findIdField​(Class<?> entityClass)
        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
      • findIsJpaEntity

        public static boolean findIsJpaEntity​(Class<?> entityClass)
        Look for a @Entity annotation on this class, or one of its superclasses. Will return true if a match is found.
        Parameters:
        entityClass - The class on which to make the determination
        Returns:
        whether or not this class is determined to be a JPA entity
      • isCreate

        public static boolean isCreate​(Object entity,
                                       @Nullable
                                       ContextInfo contextInfo)
      • setId

        public static void setId​(Object entity,
                                 Object id)
      • getAbsoluteInteger

        public static int getAbsoluteInteger​(int intValue)
        Get the absolute value of an integer. If Integer.MIN_VALUE, the absolute value would be an overflow. In this case, truncate and return Integer.MAX_VALUE instead.
        Parameters:
        intValue - The candidate integer from which to derive the absolute value
        Returns:
        The absolute value of the integer
      • optionallyTransact

        public static <P,​G extends Exception> P optionallyTransact​(org.springframework.transaction.support.TransactionTemplate template,
                                                                         EntityUtils.ThrowingReturningWork<G,​P> work)
                                                                  throws G extends Exception
        Optionally perform work inside of a transaction, if there is a valid TransactionTemplate available. This is primarly used to provide a common codebase under different transactional requirements (e.g. a JPA flow vs another db format flow).
        Type Parameters:
        P - The type to return.
        Parameters:
        template - The template to use for a transaction. Can be null.
        work - The work to perform.
        Returns:
        The result of the work.
        Throws:
        G extends Exception
      • divert

        public static <P> P divert​(@Nullable
                                   ContextInfo contextInfo,
                                   @NonNull
                                   OperationType type,
                                   @NonNull
                                   Function<ContextInfo,​P> op)
        Divert a call to the function op with the given context info and operation type. If a context info is provided, it is used as-is with only the operation type changed. Otherwise, if no context info is provided, a new ContextInfo instance is instantiated with the type provided and ignoreNarrowing set to true. Finally, before exiting, the context info (if applicable) is set back to its original operation type. This represents an attempt to utilize temporary context info mutation with as few side effects as possible. This is primarily used when there is an imminent need to demonstrate an operation type, especially when inspection of the entity data is inconclusive for operation type discovery. This is true of domain operations when the primary key is previously set. In such a case, the system cannot definitively determine if the requested operation is a CREATE or UPDATE based on primary key state alone. At that point, the system will fall back to the context info operation type, in which case, this method is of assistance.

        The behavior is pass through for origination operation types of READ and DELETE, since those types do not drive inconclusive decisions when examining data. If a ContextInfo is passed and has one of these types assigned as the operation type, it is used as-is WITHOUT mutation.
        Type Parameters:
        P - The response type
        Parameters:
        contextInfo - The current, unmodified context
        type - The operation type used to temporarily mutate the context
        op - The operation to perform under the mutated context
        Returns:
        The result of the operation