Interface Entity

All Superinterfaces:
EntityReference
All Known Subinterfaces:
EntityImplementor
All Known Implementing Classes:
AbstractDirtyCheckingEntity, AbstractEntityWrapper, AbstractHibernateEntity, AbstractPojoEntity, AbstractSessionOnlyEntity, ConditionalEntityWrapper, NullCounterEntityWrapper, NullKeyEntityWrapper, TemplateEntity, UnmodifiableEntityWrapper

public interface Entity extends EntityReference
  • Method Details Link icon

    • getKey Link icon

      @Nullable @Nullable PrimaryKey getKey()
      Get the primary key of this entity.
      Specified by:
      getKey in interface EntityReference
      Returns:
      The primary key of this entity, or null if in CONCEPTION state.
      See Also:
    • requireKey Link icon

      default PrimaryKey requireKey() throws IllegalStateException
      Get the primary key of this already persisted entity.

      Like getKey() but throws if the key is null (state CONCEPTION). Use this if you know the key has been set (ie the entity has been stored before).

      Returns:
      The primary key of this entity.
      Throws:
      IllegalStateException - if the pk is [still] null.
    • hasKey Link icon

      default boolean hasKey()
      Returns:
      true if the pk has been set. This is usually the case when the entity is not in CONCEPTION state anymore. false if the entity has a pk with value null.
    • setValue Link icon

      void setValue(String field, @Nullable @Nullable Object value)
      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.

      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 Link icon

      TypedValue<?> getValue(String field)
      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.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • requireValue Link icon

      default TypedValue<?> requireValue(String field)
      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.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
      Throws:
      IllegalStateException - if value is null
    • getValue Link icon

      @Nullable <T> T getValue(String field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      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.
    • requireValue Link icon

      default <T> T requireValue(String field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      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.
      Throws:
      IllegalStateException - if value is null
    • getValue Link icon

      @Nullable <T> T getValue(String field, Type<T> type)
      Get the value of a field as the specified type, converting the value if neccessary.
      Parameters:
      field - The name of the Field
      type - The target type.
      Returns:
      The value.
    • requireValue Link icon

      default <T> T requireValue(String field, Type<T> type)
      Get the value of a field as the specified type, converting the value if neccessary.
      Parameters:
      field - The name of the Field
      type - The target type.
      Returns:
      The value.
      Throws:
      IllegalStateException - if value is null
    • getValue Link icon

      TypedValue<?> getValue(FieldModel field)
      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.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • requireValue Link icon

      default TypedValue<?> requireValue(FieldModel field)
      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.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
      Throws:
      IllegalStateException - if value is null
    • getValue Link icon

      @Nullable <T> T getValue(FieldModel field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      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.
    • requireValue Link icon

      default <T> T requireValue(FieldModel field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      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.
      Throws:
      IllegalStateException - if value is null
    • getValue Link icon

      @Nullable <T> T getValue(FieldModel fieldModel, Type<T> type)
      Get the value of a field as the specified type, converting the value if neccessary.
      Parameters:
      fieldModel - The field model.
      type - The target type.
      Returns:
      The value.
    • requireValue Link icon

      default <T> T requireValue(FieldModel fieldModel, Type<T> type)
      Get the value of a field as the specified type, converting the value if neccessary.
      Parameters:
      fieldModel - The field model.
      type - The target type.
      Returns:
      The value.
      Throws:
      IllegalStateException - if value is null
    • delete Link icon

      void delete()
      Delete the entity.
    • getString Link icon

      @Nullable default @Nullable String getString(String field)
      Shortcut for getValue(field, String.class).
    • requireString Link icon

      default String requireString(String field)
      Shortcut for requireValue(field, String.class).
    • getDate Link icon

      @Nullable default @Nullable org.joda.time.LocalDate getDate(String field)
      Shortcut for getValue(field, LocalDate.class).
    • requireDate Link icon

      default org.joda.time.LocalDate requireDate(String field)
      Shortcut for requireValue(field, LocalDate.class).
    • getDateTime Link icon

      @Nullable default @Nullable org.joda.time.DateTime getDateTime(String field)
      Shortcut for getValue(field, DateTime.class).
    • requireDateTime Link icon

      default org.joda.time.DateTime requireDateTime(String field)
      Shortcut for requireValue(field, DateTime.class).
    • getTime Link icon

      @Nullable default @Nullable org.joda.time.LocalTime getTime(String field)
      Shortcut for getValue(field, LocalTime.class).
    • requireTime Link icon

      default org.joda.time.LocalTime requireTime(String field)
      Shortcut for requireValue(field, LocalTime.class).
    • getDecimal Link icon

      @Nullable default @Nullable BigDecimal getDecimal(String field)
      Shortcut for getValue(field, BigDecimal.class).
    • requireDecimal Link icon

      default BigDecimal requireDecimal(String field)
      Shortcut for requireValue(field, BigDecimal.class).
    • getInt Link icon

      @Nullable default @Nullable Integer getInt(String field)
      Shortcut for getValue(field, Integer.class).
    • requireInt Link icon

      default Integer requireInt(String field)
      Shortcut for requireValue(field, Integer.class).
    • getShort Link icon

      @Nullable default @Nullable Short getShort(String field)
      Shortcut for getValue(field, Short.class).
    • requireShort Link icon

      @Nullable default @Nullable Short requireShort(String field)
      Shortcut for requireValue(field, Short.class).
    • getLong Link icon

      @Nullable default @Nullable Long getLong(String field)
      Shortcut for getValue(field, Long.class).
    • requireLong Link icon

      default Long requireLong(String field)
      Shortcut for requireValue(field, Long.class).
    • getDouble Link icon

      @Nullable default @Nullable Double getDouble(String field)
      Shortcut for getValue(field, Double.class).
    • requireDouble Link icon

      default Double requireDouble(String field)
      Shortcut for requireValue(field, Double.class).
    • getFloat Link icon

      @Nullable default @Nullable Float getFloat(String field)
      Shortcut for getValue(field, Float.class).
    • requireFloat Link icon

      default Float requireFloat(String field)
      Shortcut for requireValue(field, Float.class).
    • getBool Link icon

      @Nullable default @Nullable Boolean getBool(String field)
      Shortcut for getValue(field, Boolean.class).
    • requireBool Link icon

      default Boolean requireBool(String field)
      Shortcut for requireValue(field, Boolean.class).
    • isFieldChanged Link icon

      boolean isFieldChanged(String field)
      Returns true if the current field value differs from initially loaded value in current transaction.
    • getChangedFields Link icon

      Set<String> getChangedFields()
      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.
      See Also:
    • isFieldTouched Link icon

      @Deprecated boolean isFieldTouched(String field) throws ModelException
      Deprecated.
      Throws:
      ModelException
    • getTouchedFields Link icon

      @Deprecated Set<String> getTouchedFields()
      Deprecated.
      use getChangedFields() (String)}
    • getTouchedRelations Link icon

      Set<String> getTouchedRelations()
      Returns:
      all touched relations, unmodifiableSet
    • getOldValue Link icon

      @Nullable <T> T getOldValue(String field, Class<T> targetType)
      Get the old value of a field.
      Parameters:
      field - The field to get the old value from.
      targetType - The target Java type.
      Returns:
      the old value of the field (cached value read from database).
    • resolve Link icon

      RelationQuery resolve(String relation)
      Resolve a relation. Throws if there is no such relation, check the model if you're unsure!

      The returned RelationQuery may be modified by interceptors already when you receive it. For example, in the entity system, there is (as of now) the NiceModelEntityInterceptorFactory which appends the default ordering rules of the EntityModel on query.

      Parameters:
      relation - The name of the relation
      Returns:
      The executable RelationQuery.
    • getRelatedEntity Link icon

      Entity getRelatedEntity(String relName)
      Returns the single related entity for the given relation name, or throws.
      Parameters:
      relName - The relation name.
      Returns:
      The single related entity, loaded eagerly.
      See Also:
    • getRelatedEntityOrNull Link icon

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

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

      Parameters:
      relName - The relation name.
      Returns:
      The single related entity loaded eagerly, or null if none.
      See Also:
    • joinTx Link icon

      void joinTx()
      Joins the current transaction.
    • getValueMap Link icon

      Map<String,Object> getValueMap()
      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).

      Returns:
      A map containing all values of the entity (shallow copy, modifiable).
    • isUpdatable Link icon

      boolean isUpdatable()
      Determine whether the entity is updatable.
      Returns:
      true, if the entity is updatable, false, if it's read-only.
    • getModel Link icon

      EntityModel getModel()
      Get the entity model.
      Specified by:
      getModel in interface EntityReference
      Returns:
      The entity model.
    • getManager Link icon

      EntityManager getManager()
      Get the manager that manages this entity.
    • getState Link icon

      Entity.State getState()
      Get the current state of the entity.
      Returns:
      The current state of the entity.
    • getContext Link icon

      Context getContext()
    • setAutoFieldsDisabled Link icon

      void setAutoFieldsDisabled(boolean disabled)
      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!
    • getAutoFieldsDisabled Link icon

      boolean getAutoFieldsDisabled()
      See Also:
    • validate Link icon

      CompleteEntityValidationResult validate(boolean asynchronous)
      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.
    • validate Link icon

      default CompleteEntityValidationResult validate()
      Is used for backwards compatibility. Calls validate(boolean)} with asynchronous = false.
    • getLogger Link icon

      org.slf4j.Logger getLogger()
      Provides access to the entity's logger. This may be useful to report errors on certain operation on an entity.