Class JpaUtil
java.lang.Object
com.reallifedeveloper.tools.test.database.JpaUtil
A utility class for working with JPA entities and mappings.
- Author:
- RealLifeDeveloper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddEntitiesToMapField(Field field, Object entity, List<?> entitiesToMap) Calls theputmethod on thejava.util.Mapreferenced by the given entity field to add the entities to map.static voidaddObjectToCollectionField(Field field, Object entity, Object value) Calls theaddmethod on the thejava.util.Collectionreferenced by the given entity field to add the given value.static FieldGets theFieldobject for a field with a given name in a given object, also making the field accessible by callingField.setAccessible(true.static <T> StringgetFieldName(String attributeName, Class<T> entityType) Gets the name of the field representing a given attribute in a given class or one of its superclasses.static FieldgetIdField(Object entity) Gets thd ID field of an entity, i.e., the field annotated with anIDannotation.static ObjectgetIdValue(Object entity) Gets the value of the ID field of an entity.static <ID> Class<ID> getPrimaryKeyType(Class<?> entityType) Gives the primary key class for a given entity class.static <T> StringgetTableName(Class<T> entityType) Gives the table name associated with anEntity.
-
Constructor Details
-
JpaUtil
public JpaUtil()
-
-
Method Details
-
getTableName
Gives the table name associated with anEntity.- Type Parameters:
T- the type of entity- Parameters:
entityType- the class object representingT- Returns:
- the table name associated with
entityType
-
getField
Gets theFieldobject for a field with a given name in a given object, also making the field accessible by callingField.setAccessible(true.This method never returns
null; if the field is not found, aNoSuchFieldExceptionis thrown- Parameters:
entity- the object to search for the fieldfieldName- the name of the field for which to search- Returns:
- the
Fieldobject representing the field namedfieldNameinentity - Throws:
NoSuchFieldException- if the field could not be found
-
getIdField
Gets thd ID field of an entity, i.e., the field annotated with anIDannotation.This method never returns
null; if no ID field is found, anIllegalStateExceptionis thrown.- Parameters:
entity- the entity to search- Returns:
- the
Fieldobject representing the ID field ofentity
-
getIdValue
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
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
Columnannotation with anameequal to the attribute. - The field has a
JoinColunmannotation with anameequal to the attribute. - The field name is the same as the attribute.
This method never returns
null; if no matching field is found, anIllegalArgumentExceptionis thrown.- Type Parameters:
T- the type of the entity to search- Parameters:
attributeName- the attribute for which to try to find a matching fieldentityType- 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
- The field has a
-
getPrimaryKeyType
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
Calls theaddmethod on the thejava.util.Collectionreferenced 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 collectionentity- the entity wherefieldlivesvalue- the value to add, may benull
-
addEntitiesToMapField
Calls theputmethod on thejava.util.Mapreferenced by the given entity field to add the entities to map.The key is found using the
MapKeyannotation 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 mapentity- the entity wherefieldlivesentitiesToMap- a list of entities to map
-