Class OrmTable

java.lang.Object
org.litebridge.orm.persistence.OrmTable

public class OrmTable extends Object
A table known by/registered with the ORM, facilitating the relationship between Java objects (DTOs) and database table schema.

This class maintains metadata and mappings between object field accessors and database expressions. It tracks changes made to objects and their associated database states for ORM operations.

  • Constructor Details

    • OrmTable

      public OrmTable(Class<?> dtoClass, TableMetaData metaData, Map<FieldAccessor,MappedFieldTarget> fieldAccessorTargetMap, ChangeTracker changeTracker, ClassFieldAccessorCache classFieldAccessorCache)
      Constructs a new OrmTable instance, initializing table metadata, field-to-column mappings, and a change tracker for managing object state.
      Parameters:
      dtoClass - the DTO class associated with the table
      metaData - the metadata describing the table structure
      fieldAccessorTargetMap - a map associating field accessors with their corresponding column metadata
      changeTracker - the change tracker to monitor and track modifications made to the table's data
      classFieldAccessorCache - the cache for field accessors
    • OrmTable

      public OrmTable(Class<?> dtoClass, @Nullable Class<?> contextDtoClass, TableMetaData metaData, Map<FieldAccessor,MappedFieldTarget> fieldAccessorTargetMap, ChangeTracker changeTracker, ClassFieldAccessorCache classFieldAccessorCache)
      Constructs a new OrmTable instance, initializing table metadata, field-to-column mappings, and a change tracker for managing object state.
      Parameters:
      dtoClass - the DTO class associated with the table
      contextDtoClass - Parent/context DTO class.
      metaData - the metadata describing the table structure
      fieldAccessorTargetMap - a map associating field accessors with their corresponding column metadata
      changeTracker - the change tracker to monitor and track modifications made to the table's data
      classFieldAccessorCache - the cache for field accessors
  • Method Details

    • dtoClass

      public Class<?> dtoClass()
      Get the DTO class associated with the table.
      Returns:
      the DTO class associated with the table
    • contextDtoClass

      public @Nullable Class<?> contextDtoClass()
    • dtoPkClass

      public Class<?> dtoPkClass()
    • primaryKeyFields

      public List<FieldAccessor> primaryKeyFields()
      Returns the primary key field accessors for this table.
      Returns:
      The list of primary key field accessors, or an empty list if the table has no primary key.
    • isFieldPartOfPrimaryKey

      public boolean isFieldPartOfPrimaryKey(String fieldName)
      Checks if the specified field name is mapped to a primary key column.
      Parameters:
      fieldName - The DTO field name to check.
      Returns:
      True if the field is part of the primary key, false otherwise.
    • metaData

      public TableMetaData metaData()
      Get the table metadata.
      Returns:
      the table metadata
    • columnMetaDataForField

      public ColumnMetaData columnMetaDataForField(String fieldName)
      Get the column metadata for the specified field name.
      Parameters:
      fieldName - the field name to retrieve the column metadata for
      Returns:
      the column metadata for the specified field name, or null if not found
    • columnMetaDataForField

      public ColumnMetaData columnMetaDataForField(FieldAccessor fieldAccessor)
      Get the column metadata for the specified field accessor.
      Parameters:
      fieldAccessor - the field accessor to retrieve the column metadata for
      Returns:
      the column metadata for the specified field accessor
    • hasField

      public boolean hasField(String fieldName)
    • getNestedDtoClasses

      public List<Class<?>> getNestedDtoClasses()
      Get the nested DTO classes.
      Returns:
      the list of nested DTO classes
    • getColumnMetaData

      public ColumnMetaData getColumnMetaData(String columnName)
      Get the column metadata for the specified column name.
      Parameters:
      columnName - the column name to retrieve the metadata for
      Returns:
      the column metadata for the specified column name, or null if not found
    • getTrackedDto

      public <DTO> TrackedDto<DTO> getTrackedDto(DTO dto)
      Get the tracked version of the specified DTO.
      Type Parameters:
      DTO - the type of the DTO
      Parameters:
      dto - the DTO to retrieve the tracked version for
      Returns:
      the tracked version of the specified DTO
      Throws:
      IllegalArgumentException - if the specified DTO is not tracked
    • ensureTrackedDto

      public <DTO> TrackedDto<DTO> ensureTrackedDto(DTO dto)
      Get the tracked version of the specified DTO, tracking its fields if not already tracked.
      Type Parameters:
      DTO - the type of the DTO
      Parameters:
      dto - the DTO to retrieve the tracked version for
      Returns:
      the tracked version of the specified DTO
    • trackDto

      public void trackDto(Object dto)
      Track the specified DTO's fields.
      Parameters:
      dto - the DTO to track
    • mappedFieldTargetForFieldOrNull

      public @Nullable MappedFieldTarget mappedFieldTargetForFieldOrNull(String fieldName)
      Get the mapped field target for the specified field name, or null if not found.
      Parameters:
      fieldName - the field name to retrieve the mapped target for
      Returns:
      the mapped field target, or null if not found
    • mappedFieldTargetForFieldOrNull

      public @Nullable MappedFieldTarget mappedFieldTargetForFieldOrNull(FieldAccessor fieldAccessor)
      Get the mapped field target for the specified field accessor, or null if not found.
      Parameters:
      fieldAccessor - the field accessor to retrieve the mapped target for
      Returns:
      the mapped field target, or null if not found
    • mappedFieldTargetForField

      public MappedFieldTarget mappedFieldTargetForField(String fieldName)
      Get the mapped field target for the specified field name, throwing an exception if not found.
      Parameters:
      fieldName - the field name to retrieve the mapped target for
      Returns:
      the mapped field target
      Throws:
      IllegalArgumentException - if no mapping exists for the field name
    • mappedFieldTargetForField

      public MappedFieldTarget mappedFieldTargetForField(FieldAccessor fieldAccessor)
      Get the mapped field target for the specified field accessor, throwing an exception if not found.
      Parameters:
      fieldAccessor - the field accessor to retrieve the mapped target for
      Returns:
      the mapped field target
      Throws:
      IllegalArgumentException - if no mapping exists for the field accessor
    • fieldForColumnName

      public FieldAccessor fieldForColumnName(String columnName)
      Get the field accessor for the specified column name.
      Parameters:
      columnName - the column name to retrieve the field accessor for
      Returns:
      the field accessor for the specified column name, or null if not found
      Throws:
      IllegalArgumentException - if the specified column name is null or empty
    • fieldForColumnNameOrNull

      public @Nullable FieldAccessor fieldForColumnNameOrNull(String columnName)
      Get the field accessor for the specified column name, or null if not found.
      Parameters:
      columnName - the column name to retrieve the field accessor for
      Returns:
      the field accessor for the specified column name, or null if not found
    • fieldAcessorStream

      public Stream<FieldAccessor> fieldAcessorStream()
      Get a stream of field accessors for the table.
      Returns:
      a stream of field accessors
    • syncPersistedDto

      public void syncPersistedDto(Object dto)
      Mark the specified DTO as persisted, creating a snapshot for tracking changes.
      Parameters:
      dto - the DTO to mark as persisted
      Throws:
      IllegalArgumentException - if the specified DTO is null
    • isPersistedDto

      public boolean isPersistedDto(Object dto)
      Check if the specified DTO is persisted.
      Parameters:
      dto - the DTO to check
      Returns:
      true if the specified DTO is persisted, false otherwise
      Throws:
      IllegalArgumentException - if the specified DTO is null
    • getOneToManyMappings

      public final List<MappedOneToMany> getOneToManyMappings()
      Get the one-to-many mappings for the table.
      Returns:
      the list of one-to-many mappings
    • getManyToManyMappings

      public final List<MappedManyToMany> getManyToManyMappings()
      Get the many-to-many mappings for the table.
      Returns:
      the list of many-to-many mappings
    • getOneToManyMappingForField

      public Optional<MappedOneToMany> getOneToManyMappingForField(FieldAccessor field)
      Get the one-to-many mapping for the specified field.
      Parameters:
      field - the field accessor to retrieve the mapping for
      Returns:
      the one-to-many mapping for the specified field, or empty if not found
    • getManyToManyMappingForField

      public Optional<MappedManyToMany> getManyToManyMappingForField(FieldAccessor field)
      Get the many-to-many mapping for the specified field.
      Parameters:
      field - the field accessor to retrieve the mapping for
      Returns:
      the many-to-many mapping for the specified field, or empty if not found
    • mappedFieldTargets

      public List<Map.Entry<FieldAccessor,MappedFieldTarget>> mappedFieldTargets()
      Get the mapped field targets.
      Returns:
      the list of field accessor and mapped field target entries
    • mappedColumns

      public List<ColumnMetaData> mappedColumns()
      Get the list of mapped column metadata for this table.
      Returns:
      the list of mapped column metadata
    • getContextTableRegistry

      public TableRegistry getContextTableRegistry()
      Get the context table registry.
      Returns:
      the context table registry
    • addOneToManyReverseMapping

      public void addOneToManyReverseMapping(FieldAccessor fieldAccessor)
      Add a one-to-many reverse mapping for the specified field.
      Parameters:
      fieldAccessor - the field accessor to add as a reverse mapping
    • getOneToManyReverseMappings

      public @Nullable List<FieldAccessor> getOneToManyReverseMappings()
      Get the one-to-many reverse mappings for the table.
      Returns:
      the list of one-to-many reverse mappings, or null if none
    • getDtoClassInterfaces

      public Set<Class<?>> getDtoClassInterfaces()
      Get the DTO class interfaces.
      Returns:
      the set of DTO class interfaces
    • setDtoClassInterfaces

      public void setDtoClassInterfaces(Set<Class<?>> dtoClassInterfaces)
      Set the DTO class interfaces.
      Parameters:
      dtoClassInterfaces - the set of DTO class interfaces
    • getRelatedDtoClasses

      public Set<Class<?>> getRelatedDtoClasses()
      Get the related DTO classes.
      Returns:
      the set of related DTO classes
    • isManyToManyJoinTable

      public boolean isManyToManyJoinTable()
      Check whether this table is a many-to-many join table.
      Returns:
      true if this table is a many-to-many join table; false otherwise