Interface Entity
- All Superinterfaces:
EntityReference
- All Known Subinterfaces:
EntityImplementor
- All Known Implementing Classes:
AbstractDirtyCheckingEntity,AbstractEntityWrapper,AbstractHibernateEntity,AbstractPojoEntity,AbstractSessionOnlyEntity,ConditionalEntityWrapper,NullCounterEntityWrapper,NullKeyEntityWrapper,TemplateEntity,UnmodifiableEntityWrapper
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumThe current state of the in-memory representation of an entity in respect of the database. -
Method Summary
Modifier and TypeMethodDescriptionvoiddelete()Delete the entity.booleandefault @Nullable BooleanShortcut forgetValue(field, Boolean.class).Returns all field names for fields with values differing from initially loaded value in current transaction.default @Nullable org.joda.time.LocalDateShortcut forgetValue(field, LocalDate.class).default @Nullable org.joda.time.DateTimegetDateTime(String field) Shortcut forgetValue(field, DateTime.class).default @Nullable BigDecimalgetDecimal(String field) Shortcut forgetValue(field, BigDecimal.class).default @Nullable DoubleShortcut forgetValue(field, Double.class).default @Nullable FloatShortcut forgetValue(field, Float.class).default @Nullable IntegerShortcut forgetValue(field, Integer.class).@Nullable PrimaryKeygetKey()Get the primary key of this entity.org.slf4j.LoggerProvides access to the entity's logger.default @Nullable LongShortcut forgetValue(field, Long.class).Get the manager that manages this entity.getModel()Get the entity model.<T> TgetOldValue(String field, Class<T> targetType) Get the old value of a field.getRelatedEntity(String relName) Returns the single related entity for the given relation name, or throws.@Nullable EntitygetRelatedEntityOrNull(String relName) Returns the single related entity for the given relation name, null if none, or throws.default @Nullable ShortShortcut forgetValue(field, Short.class).getState()Get the current state of the entity.default @Nullable StringShortcut forgetValue(field, String.class).default @Nullable org.joda.time.LocalTimeShortcut forgetValue(field, LocalTime.class).TypedValue<?> getValue(FieldModel field) Get the value and (nominal) type of a field.<T> TgetValue(FieldModel fieldModel, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.<T> TgetValue(FieldModel field, Class<T> targetType) Get the value of a field, converting it if necessary.TypedValue<?> Get the value and (nominal) type of a field.<T> TGet the value of a field as the specified type, converting the value if neccessary.<T> TGet the value of a field, converting it if necessary.Get all values of the entity as a map.default booleanhasKey()booleanisFieldChanged(String field) Returns true if the current field value differs from initially loaded value in current transaction.booleanDetermine whether the entity is updatable.voidjoinTx()Joins the current transaction.default BooleanrequireBool(String field) Shortcut forrequireValue(field, Boolean.class).default org.joda.time.LocalDaterequireDate(String field) Shortcut forrequireValue(field, LocalDate.class).default org.joda.time.DateTimerequireDateTime(String field) Shortcut forrequireValue(field, DateTime.class).default BigDecimalrequireDecimal(String field) Shortcut forrequireValue(field, BigDecimal.class).default DoublerequireDouble(String field) Shortcut forrequireValue(field, Double.class).default FloatrequireFloat(String field) Shortcut forrequireValue(field, Float.class).default IntegerrequireInt(String field) Shortcut forrequireValue(field, Integer.class).default PrimaryKeyGet the primary key of this already persisted entity.default LongrequireLong(String field) Shortcut forrequireValue(field, Long.class).default <T> TrequireOldValue(String field, Class<T> targetType) Get the old value of a field, converting it if necessary.default @Nullable ShortrequireShort(String field) Shortcut forrequireValue(field, Short.class).default StringrequireString(String field) Shortcut forrequireValue(field, String.class).default org.joda.time.LocalTimerequireTime(String field) Shortcut forrequireValue(field, LocalTime.class).default TypedValue<?> requireValue(FieldModel field) Get the value and (nominal) type of a field.default <T> TrequireValue(FieldModel fieldModel, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.default <T> TrequireValue(FieldModel field, Class<T> targetType) Get the value of a field, converting it if necessary.default TypedValue<?> requireValue(String field) Get the value and (nominal) type of a field.default <T> TrequireValue(String field, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.default <T> TrequireValue(String field, Class<T> targetType) Get the value of a field, converting it if necessary.Resolve a relation.voidsetAutoFieldsDisabled(boolean disabled) Disables fields listedEntityAutoField(like version, update-user and -timestamps).voidSet the value of a field.default CompleteEntityValidationResultvalidate()Is used for backwards compatibility.validate(boolean asynchronous) Validates this entity by running the validators of this entity's fields.
-
Method Details
-
getKey
Get the primary key of this entity.- Specified by:
getKeyin interfaceEntityReference- Returns:
- The primary key of this entity, or null if in CONCEPTION state.
- See Also:
-
requireKey
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
default boolean hasKey()- Returns:
trueif the pk has been set. This is usually the case when the entity is not in CONCEPTION state anymore.falseif the entity has a pk with value null.
-
setValue
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.Convertercontributed. 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. Throwsch.tocco.nice2.security.UnauthorizedExceptionIf the user does not have the permission to set/change the value.
-
getValue
Get the value and (nominal) type of a field. If the value of the specified field isnull, typed value with the field type and an associated value ofnullis returned.- Parameters:
field- The name of the field- Returns:
- A
TypedValue.
-
requireValue
Get the value and (nominal) type of a field. If the value of the specified field isnull, typed value with the field type and an associated value ofnullis returned.- Parameters:
field- The name of the field- Returns:
- A
TypedValue. - Throws:
IllegalStateException- if value is null
-
getValue
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
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
Get the value of a field as the specified type, converting the value if neccessary.- Parameters:
field- The name of the Fieldtype- The target type.- Returns:
- The value.
-
requireValue
Get the value of a field as the specified type, converting the value if neccessary.- Parameters:
field- The name of the Fieldtype- The target type.- Returns:
- The value.
- Throws:
IllegalStateException- if value is null
-
getValue
Get the value and (nominal) type of a field. If the underlying value isnull, it returns the type associated with the value ofnull.- Parameters:
field- The name of the field- Returns:
- A
TypedValue.
-
requireValue
Get the value and (nominal) type of a field. If the underlying value isnull, it returns the type associated with the value ofnull.- Parameters:
field- The name of the field- Returns:
- A
TypedValue. - Throws:
IllegalStateException- if value is null
-
getValue
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
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
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
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
void delete()Delete the entity. -
getString
Shortcut forgetValue(field, String.class). -
requireString
Shortcut forrequireValue(field, String.class). -
getDate
Shortcut forgetValue(field, LocalDate.class). -
requireDate
Shortcut forrequireValue(field, LocalDate.class). -
getDateTime
Shortcut forgetValue(field, DateTime.class). -
requireDateTime
Shortcut forrequireValue(field, DateTime.class). -
getTime
Shortcut forgetValue(field, LocalTime.class). -
requireTime
Shortcut forrequireValue(field, LocalTime.class). -
getDecimal
Shortcut forgetValue(field, BigDecimal.class). -
requireDecimal
Shortcut forrequireValue(field, BigDecimal.class). -
getInt
Shortcut forgetValue(field, Integer.class). -
requireInt
Shortcut forrequireValue(field, Integer.class). -
getShort
Shortcut forgetValue(field, Short.class). -
requireShort
Shortcut forrequireValue(field, Short.class). -
getLong
Shortcut forgetValue(field, Long.class). -
requireLong
Shortcut forrequireValue(field, Long.class). -
getDouble
Shortcut forgetValue(field, Double.class). -
requireDouble
Shortcut forrequireValue(field, Double.class). -
getFloat
Shortcut forgetValue(field, Float.class). -
requireFloat
Shortcut forrequireValue(field, Float.class). -
getBool
Shortcut forgetValue(field, Boolean.class). -
requireBool
Shortcut forrequireValue(field, Boolean.class). -
isFieldChanged
Returns true if the current field value differs from initially loaded value in current transaction. -
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:
-
getTouchedRelations
- Returns:
- all touched relations, unmodifiableSet
-
getOldValue
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).
-
requireOldValue
Get the old 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 old value of the field.
- Throws:
IllegalStateException- if value is null
-
resolve
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
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
Returns the single related entity for the given relation name, null if none, or throws.Like
getRelatedEntity(java.lang.String)but returnsnullif none.- Parameters:
relName- The relation name.- Returns:
- The single related entity loaded eagerly, or
nullif none. - See Also:
-
joinTx
void joinTx()Joins the current transaction. -
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
boolean isUpdatable()Determine whether the entity is updatable.- Returns:
true, if the entity is updatable,false, if it's read-only.
-
getModel
EntityModel getModel()Get the entity model.- Specified by:
getModelin interfaceEntityReference- Returns:
- The entity model.
-
getManager
EntityManager getManager()Get the manager that manages this entity. -
getState
Entity.State getState()Get the current state of the entity.- Returns:
- The current state of the entity.
-
getContext
Context getContext() -
setAutoFieldsDisabled
void setAutoFieldsDisabled(boolean disabled) Disables fields listedEntityAutoField(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
boolean getAutoFieldsDisabled()- See Also:
-
validate
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
Is used for backwards compatibility. Callsvalidate(boolean)} with asynchronous = false. -
getLogger
org.slf4j.Logger getLogger()Provides access to the entity's logger. This may be useful to report errors on certain operation on an entity.
-