Class PluralProperty<E,C,T>

java.lang.Object
org.fastnate.generator.context.Property<E,C>
org.fastnate.generator.context.PluralProperty<E,C,T>
Type Parameters:
E - The type of the container class
C - The type of the collection or map
T - The type of the elements in the collection
Direct Known Subclasses:
CollectionProperty, MapProperty

public abstract class PluralProperty<E,C,T> extends Property<E,C>
Base class for MapProperty and CollectionProperty.
Author:
Tobias Liefke
  • Constructor Details

    • PluralProperty

      protected PluralProperty(EntityClass<?> sourceClass, AttributeAccessor attribute, jakarta.persistence.AssociationOverride associationOverride, jakarta.persistence.AttributeOverride attributeOverride, int valueClassParamIndex)
      Creates a new property.
      Parameters:
      sourceClass - the description of the current inspected class that contains this property
      attribute - accessor to the represented attribute
      associationOverride - the configured assocation override
      attributeOverride - the configured attribute override, if we reference an ElementCollection
      valueClassParamIndex - the index of the value argument in the collection class (0 for Collection, 1 for Map}
  • Method Details

    • buildIdColumn

      protected static String buildIdColumn(AttributeAccessor attribute, jakarta.persistence.AssociationOverride override, jakarta.persistence.CollectionTable collectionMetadata, String defaultIdColumn)
      Builds the name of the column that contains the ID of the entity for the given attribute.
      Parameters:
      attribute - the accessor for the inspected attribute
      override - contains optional override options
      collectionMetadata - the default join column
      defaultIdColumn - the default name for the column, if joinColumn is empty or null
      Returns:
      the column name
    • getPropertyArgument

      protected static <T> Class<T> getPropertyArgument(AttributeAccessor attribute, Class<T> explicitClass, int argumentIndex)
      Inspects the given attribute and searches for a generic type argument.
      Parameters:
      attribute - the attribute to inspect
      explicitClass - an explicit class to use, if the metadata specified one
      argumentIndex - the index of the argument, for maps there are two: the key and the value
      Returns:
      the found class
    • hasPluralAnnotation

      protected static boolean hasPluralAnnotation(AttributeAccessor attribute)
      Indicates, that the given attribute has an annotation that indicates a plural property.
      Parameters:
      attribute - the attribute to check
      Returns:
      true if one of the plural annotations is defined for the attribute
    • addInsertExpression

      public void addInsertExpression(TableStatement statement, E entity)
      Description copied from class: Property
      Adds an expression according to the current value of the property for the given entity to an SQL insert statement.
      Overrides:
      addInsertExpression in class Property<E,C>
      Parameters:
      statement - the created statement
      entity - the inspected entity
    • buildEmbeddedProperties

      protected void buildEmbeddedProperties(EntityClass<?> sourceClass, Class<?> targetType)
      Builds the embedded properties of this property.
      Parameters:
      sourceClass - the class that contains our property
      targetType - the target type
    • createDirectValueStatement

      protected void createDirectValueStatement(StatementsWriter writer, E entity, ColumnExpression sourceId, ColumnExpression key, T value) throws IOException
      Creates the statements for a (not embeddable) value from the collection.
      Parameters:
      writer - the target of created statements
      entity - the entity that contains the collection resp. map.
      sourceId - the ID of the current entity
      key - the value of the getKeyColumn() - either the index or the map key
      value - the current value in the collection
      Throws:
      IOException - if the writer throws one
    • createValueExpression

      protected ColumnExpression createValueExpression(E entity, ColumnExpression sourceId, T value, ColumnExpression key)
      Creates a SQL expression for a value of the collection, as long as it is not embedded.
      Parameters:
      entity - the entity that contains the collection
      sourceId - the id of the entity as SQL expression
      value - the value that is written
      key - the key/index of the entity in the collection as SQL expression
      Returns:
      the expression or null if the insert is still pending
    • createValueStatement

      protected void createValueStatement(StatementsWriter writer, E entity, ColumnExpression sourceId, ColumnExpression key, T value) throws IOException
      Adds a value statement to the list of collection statements.
      Parameters:
      writer - the target of the created statements
      entity - the entity that contains the collection
      sourceId - the id of the entity as SQL expression
      key - the key/index of the entity in the collection as SQL expression
      value - the value that is written
      Throws:
      IOException - if the writer throws one
    • findEntityMappingInformation

      protected PluralProperty.EntityMappingInformation findEntityMappingInformation(AttributeAccessor attribute, jakarta.persistence.AssociationOverride associationOverride, int valueClassParamIndex)
      Builds the mapping information for a entity collection.
      Parameters:
      attribute - accessor to the represented attribute
      associationOverride - the configured assocation override
      valueClassParamIndex - the index of the value argument in the collection class (0 for Collection, 1 for Map}
      Returns:
      the mapping information
    • generatePendingStatements

      public void generatePendingStatements(StatementsWriter writer, E entity, Object writtenEntity, Object... arguments) throws IOException
      Description copied from class: Property
      Generates the update statements for an entity that are required after another entity was generated. Only called, if EntityClass.markPendingUpdates(E, V, org.fastnate.generator.context.Property<V, ?>, java.lang.Object...) was called before for writtenEntity
      Overrides:
      generatePendingStatements in class Property<E,C>
      Parameters:
      writer - the target of the generated statements
      entity - the entity that needs to be updated
      writtenEntity - the entity that exists now in the database
      arguments - additional arguments that where given to markPendingUpdates
      Throws:
      IOException - if the writer throws one
    • getKeyColumn

      protected abstract GeneratorColumn getKeyColumn()
      An optional column that contains the index / key of the values.
      Returns:
      the key column for map properties resp. the index column for list properties
    • isEmbedded

      public boolean isEmbedded()
      Indicates that this propery is a ElementCollection that references Embeddables.
      Returns:
      true if getEmbeddedProperties() returns a list of properties
    • isRequired

      public boolean isRequired()
      Description copied from class: Property
      Indicates if this property is an required field in the database (needs to exist when the insert statement is written).
      Overrides:
      isRequired in class Property<E,C>
      Returns:
      true if the field is required
    • isTableColumn

      public boolean isTableColumn()
      Description copied from class: Property
      Indicates that this property maps to a column from the parent table.
      Specified by:
      isTableColumn in class Property<E,C>
      Returns:
      true if Property.addInsertExpression(org.fastnate.generator.statements.TableStatement, E) will add the corresponding value to the given statement
    • newElement

      public T newElement()
      Tries to create a new instance of an element using the parameter-less constructor.
      Returns:
      the new element
    • writeValueExpression

      protected void writeValueExpression(TableStatement statement, T value, ColumnExpression expression)
      Writes the value expression to the statement.
      Parameters:
      statement - the current statement
      value - the written value
      expression - the expression of the value
    • getContext

      public GeneratorContext getContext()
      The current context.
    • getDialect

      public GeneratorDialect getDialect()
      The current database dialect, as defined in the context.
    • getEmbeddedProperties

      public List<Property<T,?>> getEmbeddedProperties()
      Contains all properties of an embedded element collection.
    • getEmbeddedPropertiesByName

      public Map<String,Property<T,?>> getEmbeddedPropertiesByName()
      Contains all properties of an embedded element collection by their name.
    • getRequiredEmbeddedProperties

      public List<EntityProperty<T,?>> getRequiredEmbeddedProperties()
      Contains all properties of an embedded element collection which reference a required entity.
    • getMappedId

      public String getMappedId()
      The property to use, if an id is embedded.
    • getTable

      public GeneratorTable getTable()
      The modified table.
    • getIdColumn

      public GeneratorColumn getIdColumn()
      The column that contains the id of the entity.
    • isComposition

      public boolean isComposition()
      Indicates that, according to the CascadeType, we should remove the target entities when the current entity is removed. Always true for ElementCollections.
    • getMappedBy

      public String getMappedBy()
      Indicates that this property is defined by another property on the target type.
    • getInverseProperty

      public Property<T,?> getInverseProperty()
      The opposite property of a bidirectional mapping. Can either be another PluralProperty (it the relationship is a ManyToMany) or an EntityProperty (if the relationship is a OneToMany.
    • isUseTargetTable

      public boolean isUseTargetTable()
      Indicates to use a column of the target table.
    • getValueColumn

      public GeneratorColumn getValueColumn()
      The column that contains the value (or the id of the value).
    • getValueClass

      public Class<T> getValueClass()
      The class of the value of the collection.
    • getValueConstructor

      public Constructor<T> getValueConstructor()
      The noargs constructor for the values of the collection.
    • isEntityReference

      public boolean isEntityReference()
      Indicates that entities are referenced by the collection.
    • getValueEntityClass

      public EntityClass<T> getValueEntityClass()
      The description of the valueClass, null if not an entity (or something provider specific).
    • getValueConverter

      public ValueConverter<T> getValueConverter()
      The converter for the value of the collection, null if not a primitive value.