Class EntityMetadataCollector
java.lang.Object
io.github.blyznytsiaorg.bibernate.entity.metadata.EntityMetadataCollector
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 Summary
-
Constructor Summary
ConstructorDescriptionEntityMetadataCollector
(String packageName) Constructs an EntityMetadataCollector object with the specified package name. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
checkIfColumnAnnotationIsNotOnRelation
(Field field, Class<?> entityClass) Checks if the field is annotated with relational types are also annotated with the Column annotation.private void
checkOnSufficientField
(Field field, Class<?> entityClass) Checks whether the field has both @CreationTimestamp and @UpdateTimestamp annotations simultaneously.private void
checkTableNameOnDuplicate
(Class<?> entityClass, String tableName) Checks if the table name is already associated with another entity class.Map
<Class<?>, EntityMetadata> Collects metadata for entities within the specified package.private EntityColumnDetails
createEntityColumnDetails
(Field field, Class<?> entityClass) Creates and initializes an EntityColumnDetails object based on the provided field and entity class.private ColumnMetadata
Retrieves metadata related to the column for the specified field.private CreationTimestampMetadata
getCreationTimestampMetadata
(Field field, Class<?> entityClass) Retrieves metadata for a creation timestamp field if the field is annotated with @CreationTimestamp.private GeneratedValueMetadata
getGeneratedValue
(Field field) Retrieves the metadata for a sequence generator annotation applied to the given field.private IdMetadata
Retrieves metadata related to the identifier (ID) for the specified field.private JoinColumnMetadata
getJoinColumn
(Field field) Retrieves the metadata for a JoinColumn annotation applied to the given field.private JoinTableMetadata
getJoinTable
(Field field, Class<?> entityClass) Retrieves the metadata for a JoinTable annotation applied to the given field.private ManyToManyMetadata
getManyToMany
(Field field, Class<?> entityClass) Retrieves the metadata for a ManyToMany association defined by the given field in the specified entity class.private ManyToOneMetadata
getManyToOne
(Field field) Retrieves metadata related to ManyToOne association for the specified field.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.private String
getMappedByForOneToOne
(Field field, Class<?> entityClass, OneToOne oneToOne) Retrieves metadata related to OneToOne association for the specified field.private OneToManyMetadata
getOneToMany
(Field field) Retrieves metadata related to OneToMany association for the specified field.private OneToOneMetadata
getOneToOne
(Field field, Class<?> entityClass) private SequenceGeneratorMetadata
getSequenceGenerator
(Field field) private UpdateTimestampMetadata
getUpdateTimestampMetadata
(Field field, Class<?> entityClass) Retrieves metadata for an update timestamp field if the field is annotated with @UpdateTimestamp.private boolean
Checks if the given field's type is sufficient for use with @CreationTimestamp or @UpdateTimestamp annotations.
-
Field Details
-
ERROR_MESSAGE_ON_DUPLICATE_TABLE_NAME
- See Also:
-
CANNOT_FIND_ANY_ENTITIES_ON_CLASSPATH_WITH_THIS_PACKAGE
- See Also:
-
reflections
private final org.reflections.Reflections reflections -
inMemoryEntityMetadata
-
tableNames
-
packageName
-
-
Constructor Details
-
EntityMetadataCollector
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
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
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
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
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
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
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
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
-
getGeneratedValue
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
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
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
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
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
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
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
-
getMappedByForOneToOne
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
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
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
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.
-