Class EntityClass<E>

java.lang.Object
org.fastnate.generator.context.EntityClass<E>
Type Parameters:
E - The described class

public class EntityClass<E> extends Object
Describes the DB relevant metadata of an entity class.
Author:
Tobias Liefke
  • Field Details

    • context

      private final GeneratorContext context
      The current context.
    • entityClass

      private final Class<E> entityClass
      The represented class.
    • entityConstructor

      private final Constructor<E> entityConstructor
      The JPA constructor.
    • entityName

      private final String entityName
      The entity name.
    • table

      private GeneratorTable table
      The main table of the entity.
    • accessStyle

      private AccessStyle accessStyle
      The type of access that is used for the properties (field access or bean property access).
    • inheritanceType

      private jakarta.persistence.InheritanceType inheritanceType
      The inheritance type of this class. null if no subclass is known and no inhertance is indicated (using one of the inheritance annotations).
    • parentEntityClass

      private Class<? super E> parentEntityClass
      Only used during build() to find the joinedParentClass even if that one references our class.
    • joinedParentClass

      private EntityClass<? super E> joinedParentClass
      The description of the next entity class that has InheritanceType.JOINED, if any.
    • hierarchyRoot

      private EntityClass<? super E> hierarchyRoot
      The description of the entity class that is the root of the current inheritance hierarchy.
    • discriminator

      private ColumnExpression discriminator
      The SQL expression that is used for the disciminator column of this class. null if no discriminator is used
    • discriminatorColumn

      private GeneratorColumn discriminatorColumn
      The column for discriminator. null if this class does not belong to a table that contains a discriminator column
    • primaryKeyJoinColumn

      private GeneratorColumn primaryKeyJoinColumn
      The column that contains the id of this entity if joinedParentClass is not null.
    • idProperty

      private Property<? super E,?> idProperty
      The property that contains the id for the entity. If a joinedParentClass exists, this property is the id property of the parent class.
    • uniqueProperties

      private List<SingularProperty<E,?>> uniqueProperties
      The properties that make up an additional unique identifier for the entity. Depending on GeneratorContext.getUniquePropertyQuality() and GeneratorContext.getMaxUniqueProperties() this might be null, even if there is a set of available unique properties, if the quality is not enough.
    • uniquePropertiesQuality

      private UniquePropertyQuality uniquePropertiesQuality
      Indicates the quality of uniqueProperties.
    • allUniqueProperties

      private final List<List<SingularProperty<E,?>>> allUniqueProperties
      All available sets of properties that make up an additional unique identifier for the entities.
    • properties

      private final Map<String,Property<? super E,?>> properties
      Mapping from the name of all persistent properties to their description.
    • allProperties

      private final List<Property<? super E,?>> allProperties
      All properties of this entity, including idProperty and properties from joinedParentClass.
    • additionalProperties

      private final List<Property<? super E,?>> additionalProperties
      Properties of this entity, excluding the idProperty or properties from joinedParentClass.
    • entityStates

      private final Map<Object,GenerationState> entityStates
      The states of written entities. Only interesting for pending updates and if the ID is not generated.
    • attributeOverrides

      private final Map<String,jakarta.persistence.AttributeOverride> attributeOverrides
      All attribute overriddes of this class and the parent classes.
    • associationOverrides

      private final Map<String,jakarta.persistence.AssociationOverride> associationOverrides
      All association overriddes of this class and the parent classes.
    • builtListeners

      private List<Consumer<EntityClass<E>>> builtListeners
      The list of listeners that are informed when this class has finished to build all properties.
  • Constructor Details

    • EntityClass

      EntityClass(GeneratorContext context, Class<E> entityClass)
      Creates a new description of an entity class.
      Parameters:
      context - the current context
      entityClass - the represented class
  • Method Details

    • getAccociationOverrides

      static Map<String,jakarta.persistence.AssociationOverride> getAccociationOverrides(Map<String,jakarta.persistence.AssociationOverride> surroundingOverrides, String prefix, AnnotatedElement element)
      Finds all association overrides that are attached to the given field, method or class, by taking the already defined overrides into account.
      Parameters:
      surroundingOverrides - the overrides already defined for the surrounding element
      prefix - the prefix of the element, for lookup in surroundingOverrides
      element - the annotated element
      Returns:
      a mapping from the name of the override to its definition, empty if neither AssociationOverrides nor AssociationOverride are given
    • getAttributeOverrides

      static Map<String,jakarta.persistence.AttributeOverride> getAttributeOverrides(Map<String,jakarta.persistence.AttributeOverride> surroundingOverrides, String prefix, AnnotatedElement element)
      Finds all AttributeOverrides that are attached to the given field, method or class, by taking the already defined overrides into account.
      Parameters:
      surroundingOverrides - the overrides already defined for the surrounding element
      prefix - the prefix of the element, for lookup in surroundingOverrides
      element - the annotated element
      Returns:
      a mapping from the name of the override to its definition, empty if neither AttributeOverrides nor AttributeOverride are given
    • getColumnAnnotation

      private static jakarta.persistence.Column getColumnAnnotation(AttributeAccessor attribute, Map<String,jakarta.persistence.AttributeOverride> overrides)
    • build

      void build()
      Reads the metadata from entityClass and fills all other properties. Not done in the constructor to prevent endless loops (when we reference an entity class that references us).
    • buildDiscriminator

      private void buildDiscriminator()
    • buildDiscriminator

      private ColumnExpression buildDiscriminator(EntityClass<?> c, jakarta.persistence.DiscriminatorColumn column)
    • buildGenerators

      private void buildGenerators(Class<?> c)
      Fills the generators of the context.
      Parameters:
      c - the currently inspected class
    • buildIdProperty

      private void buildIdProperty(Class<? super E> c)
      Fills the idProperty.
      Parameters:
      c - the currently inspected class
    • buildInheritance

      private void buildInheritance()
      Determine the inheritance type and discriminator properties.
    • buildOverrides

      private void buildOverrides(Class<? super E> inspectedClass)
    • buildPrimaryKeyJoinColumn

      private void buildPrimaryKeyJoinColumn()
    • buildProperties

      private void buildProperties(Class<? super E> c, Class<? super E> stopClass)
      Fills the properties.
      Parameters:
      c - the currently inspected class
      stopClass - the class in the hierarchy to stop inspecting
    • buildUniqueProperties

      private void buildUniqueProperties(jakarta.persistence.UniqueConstraint[] uniqueConstraints)
    • buildUniqueProperty

      private void buildUniqueProperty(SingularProperty<E,?> property)
    • createPostInsertStatements

      public void createPostInsertStatements(E entity, StatementsWriter writer) throws IOException
      Marks an entity as written and creates any pending update / insert statements.
      Parameters:
      entity - the entity that exists now in the database
      writer - the target of the new statements
      Throws:
      IOException - if the writer throws one
    • findHierarchyRoot

      private void findHierarchyRoot(Class<? super E> inspectedClass)
    • findIdProperty

      private boolean findIdProperty(Iterable<AttributeAccessor> declaredAttributes)
    • findPropertyByColumnName

      private SingularProperty<E,?> findPropertyByColumnName(String columnName)
    • getEntityReference

      public ColumnExpression getEntityReference(E entity, String idField, boolean whereExpression)
      Creates an expression that references the id of an entity of this class.
      Parameters:
      entity - the entity
      idField - the field that contains the id, only interesting if the id is an EmbeddedProperty
      whereExpression - indicates that the reference is used in a "where" statement
      Returns:
      the expression - either by using the unique properties or the id of the entity
    • getGeneratedIdReference

      private ColumnExpression getGeneratedIdReference(E entity, boolean whereExpression)
    • getIdColumn

      GeneratorColumn getIdColumn(AttributeAccessor attribute)
      Resolves the column for the id property of this entity class.
      Parameters:
      attribute - the referencing attribute (for evaluating annotations)
      Returns:
      the column name
      Throws:
      IllegalStateException - if the id property is not singular and no MapsId is given
    • getStateId

      private Object getStateId(E entity)
      Finds the id for the given entity, for look up in the entityStates.
      Parameters:
      entity - the entity
      Returns:
      the ID to use as key
    • inspectUniqueConstraint

      private void inspectUniqueConstraint(jakarta.persistence.UniqueConstraint constraint)
    • isBetterUniquePropertyQuality

      private boolean isBetterUniquePropertyQuality(UniquePropertyQuality foundQuality)
    • isNew

      public boolean isNew(E entity)
      Indicates that the given entity needs to be written.
      Parameters:
      entity - the entity to check
      Returns:
      true if the entity was neither written, nor exists in the database
    • markExistingEntity

      public void markExistingEntity(E entity)
      Marks a entity reference, where we don't know the ID in the database.
      Parameters:
      entity - the entity to mark
    • markPendingUpdates

      public <V> void markPendingUpdates(E pendingEntity, V entityToUpdate, Property<V,?> propertyToUpdate, Object... arguments)
      Marks an update that is necessary when an entity is written (in the future).
      Parameters:
      pendingEntity - the entity that needs to be written, before the update can take place
      entityToUpdate - the entity to update
      propertyToUpdate - the property of the entity to update
      arguments - additional arguments
    • newInstance

      public E newInstance()
      Creates a new entity of the represented class.
      Returns:
      the new entity
    • onPropertiesAvailable

      public void onPropertiesAvailable(Consumer<EntityClass<E>> listener)
      Registers a listener that is called as soon as all properties of this class are built. If the properties are already build, the listener is called immediately.
      Parameters:
      listener - called as soon as all properties are available
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getContext

      public GeneratorContext getContext()
      The current context.
    • getEntityClass

      public Class<E> getEntityClass()
      The represented class.
    • getEntityName

      public String getEntityName()
      The entity name.
    • getTable

      public GeneratorTable getTable()
      The main table of the entity.
    • getAccessStyle

      public AccessStyle getAccessStyle()
      The type of access that is used for the properties (field access or bean property access).
    • getInheritanceType

      public jakarta.persistence.InheritanceType getInheritanceType()
      The inheritance type of this class. null if no subclass is known and no inhertance is indicated (using one of the inheritance annotations).
    • getParentEntityClass

      public Class<? super E> getParentEntityClass()
      Only used during build() to find the joinedParentClass even if that one references our class.
    • getJoinedParentClass

      public EntityClass<? super E> getJoinedParentClass()
      The description of the next entity class that has InheritanceType.JOINED, if any.
    • getHierarchyRoot

      public EntityClass<? super E> getHierarchyRoot()
      The description of the entity class that is the root of the current inheritance hierarchy.
    • getDiscriminator

      public ColumnExpression getDiscriminator()
      The SQL expression that is used for the disciminator column of this class. null if no discriminator is used
    • getDiscriminatorColumn

      public GeneratorColumn getDiscriminatorColumn()
      The column for discriminator. null if this class does not belong to a table that contains a discriminator column
    • getPrimaryKeyJoinColumn

      public GeneratorColumn getPrimaryKeyJoinColumn()
      The column that contains the id of this entity if joinedParentClass is not null.
    • getIdProperty

      public Property<? super E,?> getIdProperty()
      The property that contains the id for the entity. If a joinedParentClass exists, this property is the id property of the parent class.
    • getUniqueProperties

      public List<SingularProperty<E,?>> getUniqueProperties()
      The properties that make up an additional unique identifier for the entity. Depending on GeneratorContext.getUniquePropertyQuality() and GeneratorContext.getMaxUniqueProperties() this might be null, even if there is a set of available unique properties, if the quality is not enough.
    • getUniquePropertiesQuality

      public UniquePropertyQuality getUniquePropertiesQuality()
      Indicates the quality of uniqueProperties.
    • getAllUniqueProperties

      public List<List<SingularProperty<E,?>>> getAllUniqueProperties()
      All available sets of properties that make up an additional unique identifier for the entities.
    • getProperties

      public Map<String,Property<? super E,?>> getProperties()
      Mapping from the name of all persistent properties to their description.
    • getAllProperties

      public List<Property<? super E,?>> getAllProperties()
      All properties of this entity, including idProperty and properties from joinedParentClass.
    • getAdditionalProperties

      public List<Property<? super E,?>> getAdditionalProperties()
      Properties of this entity, excluding the idProperty or properties from joinedParentClass.
    • getEntityStates

      public Map<Object,GenerationState> getEntityStates()
      The states of written entities. Only interesting for pending updates and if the ID is not generated.
    • getAttributeOverrides

      public Map<String,jakarta.persistence.AttributeOverride> getAttributeOverrides()
      All attribute overriddes of this class and the parent classes.
    • getAssociationOverrides

      public Map<String,jakarta.persistence.AssociationOverride> getAssociationOverrides()
      All association overriddes of this class and the parent classes.