Class EntityReflectionUtils

java.lang.Object
io.github.blyznytsiaorg.bibernate.utils.EntityReflectionUtils

public class EntityReflectionUtils extends Object
Utility class providing reflection-based operations for entity classes. This class offers various methods for introspecting entity classes, retrieving information about fields, annotations, and metadata, and performing tasks such as getting table names, column names, and more.
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

  • Constructor Details

    • EntityReflectionUtils

      public EntityReflectionUtils()
  • Method Details

    • table

      public static String table(Class<?> entityClass)
      Retrieves the table name associated with the given entity class. If the entity class has a Table annotation with a non-empty name, that name is returned. Otherwise, the name is derived from the entity class name by converting it from camel case to snake case.
      Parameters:
      entityClass - The class of the entity for which to retrieve the table name.
      Returns:
      The table name associated with the entity class.
    • isImmutable

      public static boolean isImmutable(Class<?> entityClass)
      Checks whether the given entity class is marked as immutable.
      Parameters:
      entityClass - The class of the entity to check.
      Returns:
      true if the entity class is marked as immutable, false otherwise.
    • isDynamicUpdate

      public static boolean isDynamicUpdate(Class<?> entityClass)
      Checks whether the given entity class is annotated with DynamicUpdate.
      Parameters:
      entityClass - The class of the entity to check.
      Returns:
      true if the entity class is annotated with DynamicUpdate, false otherwise.
    • isColumnHasAnnotation

      public static boolean isColumnHasAnnotation(Field field, Class<? extends Annotation> annotationClass)
      Checks whether the given field has the specified annotation.
      Parameters:
      field - The field to check.
      annotationClass - The class object representing the annotation to look for.
      Returns:
      true if the field has the specified annotation, false otherwise.
    • getIndexMetadata

      public static List<IndexMetadata> getIndexMetadata(Class<?> entityClass)
      Retrieves the index metadata for the specified entity class.
      Parameters:
      entityClass - The class of the entity to retrieve index metadata for.
      Returns:
      A list of index metadata for the entity, or an empty list if no index metadata is found.
    • columnName

      public static String columnName(Field field)
      Retrieves the column name for the specified field.
      Parameters:
      field - The field to retrieve the column name for.
      Returns:
      The column name, or a generated name if not specified in annotations.
    • getJoinColumnName

      private static Optional<String> getJoinColumnName(Field field)
      Retrieves the join column name for the specified field, if present.
      Parameters:
      field - The field to retrieve the join column name for.
      Returns:
      An optional containing the join column name, or empty if not specified.
    • columnName

      public static String columnName(Field declaredField, Class<?> type)
      Retrieves the column name for the specified field, considering the type of association if present.
      Parameters:
      declaredField - The field for which to retrieve the column name.
      type - The type of the field.
      Returns:
      The column name.
    • databaseTypeForInternalJavaType

      public static String databaseTypeForInternalJavaType(Field field)
      Retrieves the database type for the internal Java type of the specified field.
      Parameters:
      field - The field for which to retrieve the database type.
      Returns:
      The database type.
    • isTimeZone

      public static boolean isTimeZone(Field field)
      Checks if the specified field represents a time zone.
      Parameters:
      field - The field to check.
      Returns:
      True if the field represents a time zone, false otherwise.
    • isTimestamp

      public static boolean isTimestamp(Field field)
      Checks if the specified field represents a timestamp.
      Parameters:
      field - The field to check.
      Returns:
      True if the field represents a timestamp, false otherwise.
    • joinTableName

      public static String joinTableName(Field field)
      Retrieves the join table name specified by the @JoinTable annotation on the given field.
      Parameters:
      field - The field representing the association.
      Returns:
      The name of the join table, or null if not specified.
    • joinTableNameCorrect

      public static String joinTableNameCorrect(Field field, Class<?> entityClass)
      Retrieves the correct join table name for the specified field and entity class. If the field is annotated with @ManyToMany and has no 'mappedBy' attribute, it retrieves the join table name. Otherwise, it constructs a default name based on the entity names involved in the association.
      Parameters:
      field - The field representing the association.
      entityClass - The entity class containing the association.
      Returns:
      The join table name if specified, or the default name if applicable, otherwise null.
    • getManyManyDefaultTableName

      private static String getManyManyDefaultTableName(Field field, Class<?> entityClass)
      Constructs a default join table name for a @ManyToMany association based on the names of the entities involved.
      Parameters:
      field - The field representing the association.
      entityClass - The entity class containing the association.
      Returns:
      The default join table name.
    • tableJoinColumnNameCorrect

      public static String tableJoinColumnNameCorrect(Field field, Class<?> entityClass)
      Retrieves the correct join column name for the specified field and entity class. If the field is annotated with @ManyToMany and has no 'mappedBy' attribute, it retrieves the join column name specified in the @JoinTable annotation. Otherwise, it constructs a default name based on the entity name and its primary key column.
      Parameters:
      field - The field representing the association.
      entityClass - The entity class containing the association.
      Returns:
      The correct join column name if specified, otherwise null.
    • defaultTableJoinColumnName

      private static String defaultTableJoinColumnName(Class<?> entityClass)
      Constructs a default join column name for a @ManyToMany association based on the entity class name and its primary key column name.
      Parameters:
      entityClass - The entity class containing the association.
      Returns:
      The default join column name.
    • inverseTableJoinColumnName

      public static String inverseTableJoinColumnName(Field field)
      Retrieves the inverse join column name for the specified field, if applicable. This method is used in the context of Many-to-Many associations where 'mappedBy' is not specified.
      Parameters:
      field - The field representing the association.
      Returns:
      The inverse join column name if specified, otherwise null.
    • defaultInverseTableJoinColumnName

      public static String defaultInverseTableJoinColumnName(Field field)
      Constructs a default inverse join column name for a @ManyToMany association based on the type of the collection's generic entity class and its primary key column name.
      Parameters:
      field - The field representing the association.
      Returns:
      The default inverse join column name.
    • joinColumnJoinTableDatabaseType

      public static String joinColumnJoinTableDatabaseType(Field field, Class<?> entityClass)
      Retrieves the database type of the join column in the join table for the specified field and entity class.
      Parameters:
      field - The field representing the association.
      entityClass - The entity class containing the association.
      Returns:
      The database type of the join column if applicable, otherwise null.
    • inverseJoinColumnJoinTableDatabaseType

      public static String inverseJoinColumnJoinTableDatabaseType(Field field)
      Retrieves the database type of the inverse join column in the join table for the specified field.
      Parameters:
      field - The field representing the association.
      Returns:
      The database type of the inverse join column if applicable, otherwise null.
    • foreignKeyForJoinColumn

      public static String foreignKeyForJoinColumn(Field field)
      Retrieves the foreign key constraint name for the join column in the join table associated with the specified field.
      Parameters:
      field - The field representing the association.
      Returns:
      The foreign key constraint name for the join column if applicable, otherwise null.
    • foreignKeyForInverseJoinColumn

      public static String foreignKeyForInverseJoinColumn(Field field)
      Retrieves the foreign key constraint name for the inverse join column in the join table associated with the specified field.
      Parameters:
      field - The field representing the association.
      Returns:
      The foreign key constraint name for the inverse join column if applicable, otherwise null.
    • tableJoinColumnName

      public static String tableJoinColumnName(Field field)
      Retrieves the name of the join column in the join table associated with the specified field.
      Parameters:
      field - The field representing the association.
      Returns:
      The name of the join column if applicable, otherwise null.
    • joinColumnName

      public static String joinColumnName(Class<?> sourceType, Class<?> fieldType)
      Retrieves the name of the join column corresponding to the specified source type and field type.
      Parameters:
      sourceType - The source entity type.
      fieldType - The field type representing the association.
      Returns:
      The name of the join column if found, otherwise null.
    • joinColumnName

      public static String joinColumnName(Field field)
      Retrieves the name of the join column for the specified field.
      Parameters:
      field - The field representing the association.
      Returns:
      The name of the join column if present, otherwise a default name based on the field name.
    • databaseTypeForJoinColumn

      public static String databaseTypeForJoinColumn(Field field)
      Retrieves the database type for the join column in the specified field.
      Parameters:
      field - The field representing the association.
      Returns:
      The database type for the join column if applicable, otherwise null.
    • getTypeOfIdField

      private static Class<?> getTypeOfIdField(Class<?> fieldType)
      Retrieves the type of the field annotated with @Id within the specified class.
      Parameters:
      fieldType - The class type to search for the @Id annotation.
      Returns:
      The type of the field annotated with @Id if found, otherwise throws a MappingException.
      Throws:
      MappingException - if no field with the @Id annotation is found in the specified class.
    • columnIdName

      public static String columnIdName(Class<?> entityClass)
      Retrieves the name of the column annotated with @Id in the specified entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      The name of the column annotated with @Id if found, otherwise throws a MissingAnnotationException.
      Throws:
      MissingAnnotationException - if no field with the @Id annotation is found in the specified entity class.
    • columnVersionName

      public static String columnVersionName(Class<?> entityClass)
      Retrieves the name of the column annotated with @Version in the specified entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      The name of the column annotated with @Version if found, otherwise throws a MissingAnnotationException.
      Throws:
      MissingAnnotationException - if no field with the @Version annotation is found in the specified entity class.
    • findColumnNameByAnnotation

      private static String findColumnNameByAnnotation(Class<?> entityClass, Class<? extends Annotation> annotationClass, String errorMessage)
      Helper method to find the name of a column annotated with the specified annotation in the given entity class.
      Parameters:
      entityClass - The class representing the entity.
      annotationClass - The annotation class to search for.
      errorMessage - The error message to use if the annotation is not found.
      Returns:
      The name of the column annotated with the specified annotation if found, otherwise throws a MissingAnnotationException.
      Throws:
      MissingAnnotationException - if no field with the specified annotation is found in the specified entity class.
    • setVersionValueIfNull

      public static void setVersionValueIfNull(Class<?> entityClass, Object entity)
      Sets the version value to 1 if it is null for the specified entity or entities.
      Parameters:
      entityClass - The class representing the entity.
      entity - The entity for which to set the version value to 1 if it is null.
    • setVersionValueIfNull

      public static void setVersionValueIfNull(Class<?> entityClass, Collection<?> entities)
      Sets the version value to 1 if it is null for the specified entities.
      Parameters:
      entityClass - The class representing the entity.
      entities - The collection of entities for which to set the version value to 1 if it is null.
    • columnVersionValue

      public static Object columnVersionValue(Class<?> entityClass, Object entity)
      Retrieves the version value from the specified entity.
      Parameters:
      entityClass - The class representing the entity.
      entity - The entity from which to retrieve the version value.
      Returns:
      The version value from the entity.
    • findColumnValueByAnnotation

      private static Object findColumnValueByAnnotation(Class<?> entityClass, Class<? extends Annotation> annotationClass, Object entity)
      Retrieves the value of a column annotated with the specified annotation from the given entity.
      Parameters:
      entityClass - The class representing the entity.
      annotationClass - The class object representing the annotation.
      entity - The entity from which to retrieve the column value.
      Returns:
      The value of the column annotated with the specified annotation from the entity.
      Throws:
      MissingAnnotationException - If the annotation is not found on any field of the entity class.
    • isColumnVersionFound

      public static boolean isColumnVersionFound(Class<?> entityClass)
      Checks if the version column is found in the specified entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      True if the version column is found, otherwise false.
    • columnIdValue

      public static Object columnIdValue(Class<?> entityClass, Object entity)
      Retrieves the value of the column annotated with the @Id annotation from the given entity.
      Parameters:
      entityClass - The class representing the entity.
      entity - The entity from which to retrieve the column value.
      Returns:
      The value of the column annotated with the @Id annotation from the entity.
      Throws:
      MissingAnnotationException - If the @Id annotation is not found on any field of the entity class.
    • columnIdType

      public static Class<?> columnIdType(Class<?> entityClass)
      Retrieves the type of the column annotated with the @Id annotation in the specified entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      The type of the column annotated with the @Id annotation.
      Throws:
      MissingAnnotationException - If the @Id annotation is not found on any field of the entity class.
    • getFieldValue

      public static Object getFieldValue(Field field, Object obj)
      Retrieves the value of the specified field from the given object.
      Parameters:
      field - The field from which to retrieve the value.
      obj - The object from which to retrieve the field value.
      Returns:
      The value of the specified field in the object.
      Throws:
      BibernateGeneralException - If an error occurs while accessing the field value.
    • getValueFromObject

      public static Object getValueFromObject(Object entity, Field field)
      Retrieves the value of the specified field from the given object.
      Parameters:
      entity - The object from which to retrieve the field value.
      field - The field from which to retrieve the value.
      Returns:
      The value of the specified field in the object.
    • isToOneReference

      private static boolean isToOneReference(Field field)
      Checks if the specified field is annotated with @ManyToOne or @OneToOne.
      Parameters:
      field - The field to check.
      Returns:
      True if the field is annotated with @ManyToOne or @OneToOne, false otherwise.
    • getIdValueFromField

      public static Object getIdValueFromField(Object reference)
      Retrieves the value of the ID field from the given object.
      Parameters:
      reference - The object from which to retrieve the ID value.
      Returns:
      The value of the ID field in the object.
    • verifyIsIdHasStrategyGeneratorOrNotNullValue

      public static <T> void verifyIsIdHasStrategyGeneratorOrNotNullValue(T reference)
      Verifies whether the ID field of the given reference object has a strategy generator or a non-null value.
      Type Parameters:
      T - The type of the reference object.
      Parameters:
      reference - The reference object to verify.
    • verifyIsIdHasStrategyGeneratorOrNotNullValue

      public static <T> void verifyIsIdHasStrategyGeneratorOrNotNullValue(Collection<T> references)
      Verifies whether the ID field of each reference object in the collection has a strategy generator or a non-null value.
      Type Parameters:
      T - The type of the reference objects in the collection.
      Parameters:
      references - The collection of reference objects to verify.
    • setValueForObject

      public static void setValueForObject(Object entity, Field field, Object value)
      Sets the value of the specified field in the given object using reflection.
      Parameters:
      entity - The object whose field's value needs to be set.
      field - The field whose value needs to be set.
      value - The value to set.
    • getValueFromResultSetByColumn

      public static Object getValueFromResultSetByColumn(ResultSet resultSet, String joinColumnName)
      Retrieves a value from a ResultSet based on the specified column name.
      Parameters:
      resultSet - The ResultSet from which to retrieve the value.
      joinColumnName - The name of the column from which to retrieve the value.
      Returns:
      The value retrieved from the ResultSet.
      Throws:
      BibernateGeneralException - If an error occurs while retrieving the value from the ResultSet.
    • getValueFromResultSet

      public static Object getValueFromResultSet(Field field, ResultSet resultSet, String fieldName)
      Retrieves a value from a ResultSet based on the specified field type and field name.
      Parameters:
      field - The field object representing the field type.
      resultSet - The ResultSet from which to retrieve the value.
      fieldName - The name of the field from which to retrieve the value.
      Returns:
      The value retrieved from the ResultSet.
    • getEntityId

      public static Object getEntityId(Field field, ResultSet resultSet)
      Retrieves the ID value of an entity from a ResultSet based on the field representing the entity's ID.
      Parameters:
      field - The field representing the ID of the entity.
      resultSet - The ResultSet containing the entity data.
      Returns:
      The ID value of the entity, or null if not found or if an SQL exception occurs.
    • getDifference

      public static List<ColumnSnapshot> getDifference(List<ColumnSnapshot> currentEntitySnapshot, List<ColumnSnapshot> oldEntitySnapshot)
      Compares two lists of ColumnSnapshot objects representing entity snapshots and returns the differences.
      Parameters:
      currentEntitySnapshot - The list of ColumnSnapshot objects representing the current entity snapshot.
      oldEntitySnapshot - The list of ColumnSnapshot objects representing the old entity snapshot.
      Returns:
      A list of ColumnSnapshot objects representing the differences between the two snapshots.
    • setField

      public static void setField(Field field, Object obj, Object value)
      Sets the value of a field in an object.
      Parameters:
      field - The field to set.
      obj - The object whose field value will be set.
      value - The value to set in the field.
      Throws:
      BibernateGeneralException - If an error occurs while setting the field value.
    • castIdToEntityId

      public static <T> T castIdToEntityId(Class<T> entityClass, Object primaryKey)
      Casts the primary key value to the appropriate type for an entity's ID field.
      Type Parameters:
      T - The type of the entity ID.
      Parameters:
      entityClass - The class of the entity.
      primaryKey - The primary key value to cast.
      Returns:
      The primary key value casted to the appropriate entity ID type.
      Throws:
      NullPointerException - If either entityClass or primaryKey is null.
    • getCollectionGenericType

      public static Class<?> getCollectionGenericType(Field field)
      Retrieves the generic type of a collection field.
      Parameters:
      field - The field representing the collection.
      Returns:
      The class representing the generic type of the collection.
      Throws:
      BibernateGeneralException - If the field is not a supported collection type.
    • isSupportedCollection

      public static boolean isSupportedCollection(Field field)
      Checks if a field represents a supported collection type.
      Parameters:
      field - The field to check.
      Returns:
      True if the field represents a supported collection type; false otherwise.
    • getInsertEntityFields

      public static List<Field> getInsertEntityFields(Class<?> entityClass)
      Retrieves fields of an entity that can be used during insert operations. These are fields that do not have special annotations like @GeneratedValue, @OneToOne without @JoinColumn, ManyToOne without @JoinColumn, @OneToMany, @CreationTimestamp, @ManyToMany, or @UpdateTimestamp.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      A list of fields suitable for insert operations.
    • getEntityFields

      public static List<EntityColumn> getEntityFields(Class<?> entityClass)
      Retrieves a list of EntityColumn objects representing fields of the given entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      A list of EntityColumn objects containing information about the entity fields.
    • getIdField

      public static Field getIdField(Class<?> entityClass)
      Retrieves the field annotated with @Id in the given entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      The field annotated with @Id.
      Throws:
      MissingAnnotationException - If no field annotated with @Id is found in the entity class.
    • getManyToManyWithJoinTableFields

      public static List<Field> getManyToManyWithJoinTableFields(Class<?> entityClass)
      Retrieves fields annotated with @ManyToMany and @JoinTable in the given entity class.
      Parameters:
      entityClass - The class representing the entity.
      Returns:
      A list of fields annotated with @ManyToMany and @JoinTable.
    • setIdField

      public static Object setIdField(Object entity, Object value)
      Sets the value of the field annotated with @Id in the given entity object.
      Parameters:
      entity - The entity object.
      value - The value to set.
      Returns:
      The modified entity object.
    • getGeneratedValueField

      public static Field getGeneratedValueField(Object entity)
      Retrieves the field annotated with @GeneratedValue in the given entity object.
      Parameters:
      entity - The entity object.
      Returns:
      The field annotated with @GeneratedValue.
      Throws:
      MissingAnnotationException - If no field annotated with @GeneratedValue is found in the entity object.
    • getGeneratedValueSequenceConfig

      public static SequenceConf getGeneratedValueSequenceConfig(Class<?> entityClass, String tableName)
      Retrieves the sequence configuration for a field annotated with @GeneratedValue and strategy set to "SEQUENCE" in the given entity class.
      Parameters:
      entityClass - The class of the entity.
      tableName - The name of the table associated with the sequence.
      Returns:
      The sequence configuration.
      Throws:
      MissingAnnotationException - If no such field with the specified sequence strategy is found in the entity class.
    • isAnnotationPresent

      public static boolean isAnnotationPresent(Field field, Class<? extends Annotation> annotationClass)
      Checks if a field has a specific annotation present.
      Parameters:
      field - The field to check.
      annotationClass - The annotation class to check for.
      Returns:
      True if the field has the specified annotation; otherwise, false.
    • getSequenceConfFromField

      private static SequenceConf getSequenceConfFromField(Field field, String tableName)
      Retrieves the sequence configuration from a field annotated with @GeneratedValue and @SequenceGenerator if the generator name matches the sequence generator name. Otherwise, it constructs a default sequence name based on the table and column name.
      Parameters:
      field - The field annotated with @GeneratedValue and @SequenceGenerator.
      tableName - The name of the table associated with the sequence.
      Returns:
      The sequence configuration.
    • isBidirectional

      public static boolean isBidirectional(Field field)
      Checks if a field is part of a bidirectional relationship by inspecting its annotations.
      Parameters:
      field - The field to check for bidirectionality.
      Returns:
      True if the field is part of a bidirectional relationship; otherwise, false.
    • convertToType

      private static Object convertToType(Object value, Class<?> targetType)
      Converts a value to the specified target type. Supports conversion from Number to numeric types, Boolean to Boolean, Character to Character, and String to Long.
      Parameters:
      value - The value to be converted.
      targetType - The target type to which the value will be converted.
      Returns:
      The converted value.
    • getSnakeString

      public static String getSnakeString(String str)
      Converts a given string to snake case.
      Parameters:
      str - The input string.
      Returns:
      The string converted to snake case.