Class EntityMetadataCollector

java.lang.Object
io.github.blyznytsiaorg.bibernate.entity.metadata.EntityMetadataCollector

public class EntityMetadataCollector extends Object
The EntityMetadataCollector class is responsible for collecting metadata for entities within a specified package. It utilizes reflection to scan for classes annotated with @Entity and gathers metadata such as table names, column details and index metadata.
Since:
1.0
Author:
Blyzhnytsia Team
See Also:
  • Field Details

    • ERROR_MESSAGE_ON_DUPLICATE_TABLE_NAME

      public static final String ERROR_MESSAGE_ON_DUPLICATE_TABLE_NAME
      See Also:
    • CANNOT_FIND_ANY_ENTITIES_ON_CLASSPATH_WITH_THIS_PACKAGE

      public static final String CANNOT_FIND_ANY_ENTITIES_ON_CLASSPATH_WITH_THIS_PACKAGE
      See Also:
    • reflections

      private final org.reflections.Reflections reflections
    • inMemoryEntityMetadata

      private final Map<Class<?>,EntityMetadata> inMemoryEntityMetadata
    • tableNames

      private final HashMap<String,Class<?>> tableNames
    • packageName

      private final String packageName
  • Constructor Details

    • EntityMetadataCollector

      public EntityMetadataCollector(String packageName)
      Constructs an EntityMetadataCollector object with the specified package name. It initializes the Reflections object to scan the package for entity classes and initializes internal data structures to store metadata.
      Parameters:
      packageName - The name of the package to scan for entity classes.
  • Method Details

    • collectMetadata

      public Map<Class<?>,EntityMetadata> collectMetadata()
      Collects metadata for entities within the specified package. It scans for classes annotated with @Entity using reflection, gathers metadata such as table names, column details, index metadata and stores them in a map.
      Returns:
      A map containing entity classes as keys and their corresponding metadata as values.
      Throws:
      EntitiesNotFoundException - if no entities are found in the specified package.
    • checkTableNameOnDuplicate

      private void checkTableNameOnDuplicate(Class<?> entityClass, String tableName)
      Checks if the table name is already associated with another entity class. If a duplicate table name is found, it throws a MappingException.
      Parameters:
      entityClass - The entity class for which the table name is being checked.
      tableName - The table name to check for duplicates.
      Throws:
      MappingException - if a duplicate table name is found.
    • createEntityColumnDetails

      private EntityColumnDetails createEntityColumnDetails(Field field, Class<?> entityClass)
      Creates and initializes an EntityColumnDetails object based on the provided field and entity class.
      Parameters:
      field - The field for which EntityColumnDetails is being created.
      entityClass - The entity class to which the field belongs.
      Returns:
      An initialized EntityColumnDetails object.
    • getUpdateTimestampMetadata

      private UpdateTimestampMetadata getUpdateTimestampMetadata(Field field, Class<?> entityClass)
      Retrieves metadata for an update timestamp field if the field is annotated with @UpdateTimestamp.
      Parameters:
      field - The field to check for the @UpdateTimestamp annotation.
      entityClass - The class to which the field belongs.
      Returns:
      UpdateTimestampMetadata object if the field has the @UpdateTimestamp annotation; otherwise, null.
    • getCreationTimestampMetadata

      private CreationTimestampMetadata getCreationTimestampMetadata(Field field, Class<?> entityClass)
      Retrieves metadata for a creation timestamp field if the field is annotated with @CreationTimestamp.
      Parameters:
      field - The field to check for the @CreationTimestamp annotation.
      entityClass - The class to which the field belongs.
      Returns:
      CreationTimestampMetadata object if the field has the @CreationTimestamp annotation; otherwise, null.
    • checkOnSufficientField

      private void checkOnSufficientField(Field field, Class<?> entityClass)
      Checks whether the field has both @CreationTimestamp and @UpdateTimestamp annotations simultaneously. Additionally, it verifies if the field's type is suitable for @CreationTimestamp or @UpdateTimestamp annotations.
      Parameters:
      field - The field to check for annotations and type suitability.
      entityClass - The class to which the field belongs.
      Throws:
      MappingException - if the field has both @CreationTimestamp and @UpdateTimestamp annotations simultaneously, or if the field's type is not suitable for @CreationTimestamp or @UpdateTimestamp annotations.
    • isJavaTypeSufficientForTimestamps

      private boolean isJavaTypeSufficientForTimestamps(Field field)
      Checks if the given field's type is sufficient for use with @CreationTimestamp or @UpdateTimestamp annotations. Supported types include LocalDate, OffsetTime, LocalTime, OffsetDateTime, and LocalDateTime.
      Parameters:
      field - The field to check.
      Returns:
      true if the field's type is one of the supported timestamp types, false otherwise.
    • getSequenceGenerator

      private SequenceGeneratorMetadata getSequenceGenerator(Field field)
    • getGeneratedValue

      private GeneratedValueMetadata getGeneratedValue(Field field)
      Retrieves the metadata for a sequence generator annotation applied to the given field.
      Parameters:
      field - The field to inspect for the presence of a SequenceGenerator annotation.
      Returns:
      SequenceGeneratorMetadata if the field is annotated with SequenceGenerator, null otherwise.
    • getJoinTable

      private JoinTableMetadata getJoinTable(Field field, Class<?> entityClass)
      Retrieves the metadata for a JoinTable annotation applied to the given field.
      Parameters:
      field - The field to inspect for the presence of a JoinTable annotation.
      entityClass - The class of the entity to which the field belongs.
      Returns:
      JoinTableMetadata if the field is annotated with JoinTable and is a ManyToMany association, null otherwise.
      Throws:
      MappingException - if the field is annotated with JoinTable but not annotated with ManyToMany.
    • getJoinColumn

      private JoinColumnMetadata getJoinColumn(Field field)
      Retrieves the metadata for a JoinColumn annotation applied to the given field.
      Parameters:
      field - The field to inspect for the presence of a JoinColumn annotation.
      Returns:
      JoinColumnMetadata if the field is annotated with JoinColumn and is a OneToOne or ManyToOne association, null otherwise.
      Throws:
      MappingException - if the field is annotated with JoinColumn but not annotated with OneToOne or ManyToOne.
    • getManyToMany

      private ManyToManyMetadata getManyToMany(Field field, Class<?> entityClass)
      Retrieves the metadata for a ManyToMany association defined by the given field in the specified entity class.
      Parameters:
      field - The field representing the ManyToMany association.
      entityClass - The entity class containing the ManyToMany association.
      Returns:
      ManyToManyMetadata if the field is annotated with ManyToMany, null otherwise.
    • getMappedByForManyToMany

      private String getMappedByForManyToMany(Field field, Class<?> entityClass, ManyToMany manyToMany)
      Retrieves the mappedBy attribute value for a ManyToMany association defined by the given field in the specified entity class.
      Parameters:
      field - The field representing the ManyToMany association.
      entityClass - The entity class containing the ManyToMany association.
      manyToMany - The ManyToMany annotation instance.
      Returns:
      The mappedBy attribute value if not empty and valid, otherwise an empty string.
      Throws:
      MappingException - If the mappedBy attribute is not found in the target entity.
    • getManyToOne

      private ManyToOneMetadata getManyToOne(Field field)
      Retrieves metadata related to ManyToOne association for the specified field.
      Parameters:
      field - The field representing the ManyToOne association.
      Returns:
      Metadata related to ManyToOne association if the field is annotated with ManyToOne, otherwise null.
    • getOneToMany

      private OneToManyMetadata getOneToMany(Field field)
      Retrieves metadata related to OneToMany association for the specified field.
      Parameters:
      field - The field representing the OneToMany association.
      Returns:
      Metadata related to OneToMany association if the field is annotated with OneToMany, otherwise null.
    • getOneToOne

      private OneToOneMetadata getOneToOne(Field field, Class<?> entityClass)
    • getMappedByForOneToOne

      private String getMappedByForOneToOne(Field field, Class<?> entityClass, OneToOne oneToOne)
      Retrieves metadata related to OneToOne association for the specified field.
      Parameters:
      field - The field representing the OneToOne association.
      entityClass - The class of the entity containing the field.
      Returns:
      Metadata related to OneToOne association if the field is annotated with OneToOne, otherwise null.
    • getId

      private IdMetadata getId(Field field)
      Retrieves metadata related to the identifier (ID) for the specified field.
      Parameters:
      field - The field for which ID metadata is retrieved.
      Returns:
      Metadata related to the ID if the field is annotated with @Id, otherwise null.
    • getColumn

      private ColumnMetadata getColumn(Field field, Class<?> entityClass)
      Retrieves metadata related to the column for the specified field.
      Parameters:
      field - The field for which column metadata is retrieved.
      Returns:
      Metadata related to the column.
    • checkIfColumnAnnotationIsNotOnRelation

      private void checkIfColumnAnnotationIsNotOnRelation(Field field, Class<?> entityClass)
      Checks if the field is annotated with relational types are also annotated with the Column annotation.
      Parameters:
      field - The field to check for annotations.
      entityClass - The class containing the field.
      Throws:
      MappingException - If the field is annotated with both relational and column annotations. The exception message indicates the name of the field and the class where the violation occurred.