java.lang.Object
com.reallifedeveloper.tools.test.database.JpaUtil

public class JpaUtil extends Object
A utility class for working with JPA entities and mappings.
Author:
RealLifeDeveloper
  • Constructor Details

    • JpaUtil

      public JpaUtil()
  • Method Details

    • getTableName

      public static <T> String getTableName(Class<T> entityType)
      Gives the table name associated with an Entity.
      Type Parameters:
      T - the type of entity
      Parameters:
      entityType - the class object representing T
      Returns:
      the table name associated with entityType
    • getField

      public static Field getField(Object entity, String fieldName) throws NoSuchFieldException
      Gets the Field object for a field with a given name in a given object, also making the field accessible by calling Field.setAccessible(true.

      This method never returns null; if the field is not found, a NoSuchFieldException is thrown

      Parameters:
      entity - the object to search for the field
      fieldName - the name of the field for which to search
      Returns:
      the Field object representing the field named fieldName in entity
      Throws:
      NoSuchFieldException - if the field could not be found
    • getIdField

      public static Field getIdField(Object entity)
      Gets thd ID field of an entity, i.e., the field annotated with an ID annotation.

      This method never returns null; if no ID field is found, an IllegalStateException is thrown.

      Parameters:
      entity - the entity to search
      Returns:
      the Field object representing the ID field of entity
    • getIdValue

      public static Object getIdValue(Object entity) throws IllegalAccessException
      Gets the value of the ID field of an entity.
      Parameters:
      entity - the entity to search
      Returns:
      the value of the ID Field
      Throws:
      IllegalAccessException - if there was a problem getting the value using reflection
    • getFieldName

      public static <T> String getFieldName(String attributeName, Class<T> entityType)
      Gets the name of the field representing a given attribute in a given class or one of its superclasses.

      A field is considered to represent an attribute if one of the following is true:

      • The field has a Column annotation with a name equal to the attribute.
      • The field has a JoinColunm annotation with a name equal to the attribute.
      • The field name is the same as the attribute.

      This method never returns null; if no matching field is found, an IllegalArgumentException is thrown.

      Type Parameters:
      T - the type of the entity to search
      Parameters:
      attributeName - the attribute for which to try to find a matching field
      entityType - the class in which to search for the field, continuing with superclasses if necessary
      Returns:
      the name of the field representing attributeName
      Throws:
      IllegalArgumentException - if no matching field could be found
    • getPrimaryKeyType

      public static <ID> Class<ID> getPrimaryKeyType(Class<?> entityType)
      Gives the primary key class for a given entity class.
      Type Parameters:
      ID - the type of the primary key
      Parameters:
      entityType - the class object representing the entity
      Returns:
      the primary key class of entityType
    • addObjectToCollectionField

      public static void addObjectToCollectionField(Field field, Object entity, Object value)
      Calls the add method on the the java.util.Collection referenced by the given entity field to add the given value.

      This method should only be called when you know that the field actually is a collection.

      Parameters:
      field - the field holding the collection
      entity - the entity where field lives
      value - the value to add, may be null
    • addEntitiesToMapField

      public static void addEntitiesToMapField(Field field, Object entity, List<?> entitiesToMap)
      Calls the put method on the java.util.Map referenced by the given entity field to add the entities to map.

      The key is found using the MapKey annotation of the field.

      This method should only be called when you know that the field actually is a map.

      Parameters:
      field - the field holding the map
      entity - the entity where field lives
      entitiesToMap - a list of entities to map