Class AbstractHibernateEntity

java.lang.Object
ch.tocco.nice2.persist.core.impl.hibernate.pojo.AbstractHibernateEntity
All Implemented Interfaces:
EntityReference, Entity, EntityImplementor
Direct Known Subclasses:
AbstractDirtyCheckingEntity, AbstractSessionOnlyEntity

public abstract class AbstractHibernateEntity extends Object implements EntityImplementor
A base class for dynamically created Entity instances (see JavassistEntityPojoFactory Used for XML based entities.
  • Field Details

    • logger

      protected final org.slf4j.Logger logger
    • entityContext

      protected EntityContext entityContext
    • entityInterceptor

      protected EntityInterceptor entityInterceptor
    • wasDeleted

      protected boolean wasDeleted
  • Constructor Details

    • AbstractHibernateEntity

      public AbstractHibernateEntity()
  • Method Details

    • setEntityContext

      public void setEntityContext(EntityContext entityContext)
    • toString

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

      public abstract EntityInterceptor getInnerInterceptor()
    • internalGetValue

      @Nullable protected <T> T internalGetValue(String fieldName, Class<T> type)
    • internalSetValue

      protected void internalSetValue(String field, Object value)
    • thisInstanceOrProxyIfAvailable

      protected Entity thisInstanceOrProxyIfAvailable()
      Returns the proxy instance for this entity, if one exists in the current session. If a proxy exists, it should always be passed to the listeners instead of the actual instance. Otherwise unexpected side effects may happen when there are 2 different instances for the same entity.
    • resolveField

      protected String resolveField(String fieldName, EntityInterceptor.AccessType accessType)
    • setValue

      public void setValue(String field, @Nullable @Nullable Object value)
      Description copied from interface: Entity
      Set the value of a field.

      In the current implementation no content validation is made on this method call - other than null on a non-nullable field. Validation happens when storing, when all values have been set.

      The value must either be in the correct data type, or convertible. To be convertible there must be a ch.tocco.nice2.types.spi.Converter contributed. Note that not all kinds of conversions that seem obvious at first are registered, for example there is no string to integer converter and thus a value like "" won't work and set 0 on an int field. Setting an int 0 on a long field works though because there is an int to long type converter.

      Specified by:
      setValue in interface Entity
      Parameters:
      field - The name of the field.
      value - The new value in the expected data type, or in a conversible type of it. Throws ch.tocco.nice2.security.UnauthorizedException If the user does not have the permission to set/change the value.
    • getValue

      public TypedValue<?> getValue(String field)
      Description copied from interface: Entity
      Get the value and (nominal) type of a field. If the value of the specified field is null, typed value with the field type and an associated value of null is returned.
      Specified by:
      getValue in interface Entity
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • getValue

      @Nullable public <T> T getValue(String field, Class<T> targetType)
      Description copied from interface: Entity
      Get the value of a field, converting it if necessary.
      Specified by:
      getValue in interface Entity
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
    • getValue

      @Nullable public <T> T getValue(String field, Type<T> type)
      Description copied from interface: Entity
      Get the value of a field as the specified type, converting the value if neccessary.
      Specified by:
      getValue in interface Entity
      Parameters:
      field - The name of the Field
      type - The target type.
      Returns:
      The value.
    • getValue

      public TypedValue<?> getValue(FieldModel field)
      Description copied from interface: Entity
      Get the value and (nominal) type of a field. If the underlying value is null, it returns the type associated with the value of null.
      Specified by:
      getValue in interface Entity
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • getValue

      @Nullable public <T> T getValue(FieldModel field, Class<T> targetType)
      Description copied from interface: Entity
      Get the value of a field, converting it if necessary.
      Specified by:
      getValue in interface Entity
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
    • getValue

      @Nullable public <T> T getValue(FieldModel fieldModel, Type<T> type)
      Description copied from interface: Entity
      Get the value of a field as the specified type, converting the value if neccessary.
      Specified by:
      getValue in interface Entity
      Parameters:
      fieldModel - The field model.
      type - The target type.
      Returns:
      The value.
    • delete

      public void delete()
      Description copied from interface: Entity
      Delete the entity.
      Specified by:
      delete in interface Entity
    • markDeleted

      public void markDeleted()
      Description copied from interface: EntityImplementor
      Mark an entity as deleted. This can be used when an entity is deleted directly using a SQL or HQL query to synchronize the state of the entity with the database. This will cause that Entity.getState() will return Entity.State.PHANTOM. This is mainly used internally. Normally Entity.delete() should be used.
      Specified by:
      markDeleted in interface EntityImplementor
    • isFieldChanged

      public boolean isFieldChanged(String field)
      Description copied from interface: Entity
      Returns true if the current field value differs from initially loaded value in current transaction.
      Specified by:
      isFieldChanged in interface Entity
    • getChangedFields

      public Set<String> getChangedFields()
      Description copied from interface: Entity
      Returns all field names for fields with values differing from initially loaded value in current transaction. Note: This method throws an UnauthorizedException when at least one changed field is not readable.
      Specified by:
      getChangedFields in interface Entity
      See Also:
    • isFieldTouched

      public boolean isFieldTouched(String field) throws ModelException
      Specified by:
      isFieldTouched in interface Entity
      Throws:
      ModelException
    • getTouchedFields

      public Set<String> getTouchedFields()
      Specified by:
      getTouchedFields in interface Entity
    • getTouchedRelations

      public Set<String> getTouchedRelations()
      Specified by:
      getTouchedRelations in interface Entity
      Returns:
      all touched relations, unmodifiableSet
    • getTouchedProperties

      protected abstract Set<String> getTouchedProperties(Function<String,Boolean> filter)
    • syncInverseCollection

      public abstract void syncInverseCollection(RelationModel relationModel, Entity entity, boolean added)
    • getRelatedEntity

      public Entity getRelatedEntity(String relName)
      Description copied from interface: Entity
      Returns the single related entity for the given relation name, or throws.
      Specified by:
      getRelatedEntity in interface Entity
      Parameters:
      relName - The relation name.
      Returns:
      The single related entity, loaded eagerly.
      See Also:
    • getRelatedEntityOrNull

      @Nullable public @Nullable Entity getRelatedEntityOrNull(String relName)
      Description copied from interface: Entity
      Returns the single related entity for the given relation name, null if none, or throws.

      Like Entity.getRelatedEntity(java.lang.String) but returns null if none.

      Specified by:
      getRelatedEntityOrNull in interface Entity
      Parameters:
      relName - The relation name.
      Returns:
      The single related entity loaded eagerly, or null if none.
      See Also:
    • joinTx

      public void joinTx()
      Description copied from interface: Entity
      Joins the current transaction.
      Specified by:
      joinTx in interface Entity
    • getValueMap

      public Map<String,Object> getValueMap()
      Description copied from interface: Entity
      Get all values of the entity as a map.

      Changes to that map will not be reflected in the entity.

      The data includes pk fields as well as nice-specific fields (version etc).

      Specified by:
      getValueMap in interface Entity
      Returns:
      A map containing all values of the entity (shallow copy, modifiable).
    • isUpdatable

      public boolean isUpdatable()
      Description copied from interface: Entity
      Determine whether the entity is updatable.
      Specified by:
      isUpdatable in interface Entity
      Returns:
      true, if the entity is updatable, false, if it's read-only.
    • getModel

      public EntityModel getModel()
      Description copied from interface: Entity
      Get the entity model.
      Specified by:
      getModel in interface Entity
      Specified by:
      getModel in interface EntityReference
      Returns:
      The entity model.
    • getManager

      public EntityManager getManager()
      Description copied from interface: Entity
      Get the manager that manages this entity.
      Specified by:
      getManager in interface Entity
    • getContext

      public Context getContext()
      Specified by:
      getContext in interface Entity
    • setAutoFieldsDisabled

      public void setAutoFieldsDisabled(boolean disabled)
      Description copied from interface: Entity
      Disables fields listed EntityAutoField (like version, update-user and -timestamps). Useful for background-tasks which update things which can be re-written when overwritten by someone else. Important: Use this only when you exactly know why you are doing it!
      Specified by:
      setAutoFieldsDisabled in interface Entity
    • getAutoFieldsDisabled

      public boolean getAutoFieldsDisabled()
      Specified by:
      getAutoFieldsDisabled in interface Entity
      See Also:
    • validate

      public CompleteEntityValidationResult validate(boolean asynchronous)
      Description copied from interface: Entity
      Validates this entity by running the validators of this entity's fields. Such validation includes business logic. Data type validation has been made way before, before setting the values on this entity object. Validators that throw a WARN level error may be ignored, which means they won't be run at all, and thus won't stop a persist operation. The DataOperator uses this functionality. The GUI can show a warning to the user, and he may change his input, or ignore the warning and submit again. If asynchronous is set to true, only validators that are deemed save for asynchronous validation (e.g. validation of an entity before everything is filled in / before listeners) are run. If it is set to false, all validators will be run.
      Specified by:
      validate in interface Entity
    • getLogger

      public org.slf4j.Logger getLogger()
      Description copied from interface: Entity
      Provides access to the entity's logger. This may be useful to report errors on certain operation on an entity.
      Specified by:
      getLogger in interface Entity