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
Modifier and TypeInterfaceDescriptionstatic enum
The current state of the in-memory representation of an entity in respect of the database. -
Method Summary
Modifier and TypeMethodDescriptionvoid
delete()
Delete the entity.boolean
default @Nullable Boolean
Shortcut 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.LocalDate
Shortcut forgetValue(field, LocalDate.class)
.default @Nullable org.joda.time.DateTime
getDateTime
(String field) Shortcut forgetValue(field, DateTime.class)
.default @Nullable BigDecimal
getDecimal
(String field) Shortcut forgetValue(field, BigDecimal.class)
.default @Nullable Double
Shortcut forgetValue(field, Double.class)
.default @Nullable Float
Shortcut forgetValue(field, Float.class)
.default @Nullable Integer
Shortcut forgetValue(field, Integer.class)
.@Nullable PrimaryKey
getKey()
Get the primary key of this entity.org.slf4j.Logger
Provides access to the entity's logger.default @Nullable Long
Shortcut forgetValue(field, Long.class)
.Get the manager that manages this entity.getModel()
Get the entity model.<T> T
getOldValue
(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 Entity
getRelatedEntityOrNull
(String relName) Returns the single related entity for the given relation name, null if none, or throws.default @Nullable Short
Shortcut forgetValue(field, Short.class)
.getState()
Get the current state of the entity.default @Nullable String
Shortcut forgetValue(field, String.class)
.default @Nullable org.joda.time.LocalTime
Shortcut forgetValue(field, LocalTime.class)
.Deprecated.TypedValue
<?> getValue
(FieldModel field) Get the value and (nominal) type of a field.<T> T
getValue
(FieldModel fieldModel, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.<T> T
getValue
(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> T
Get the value of a field as the specified type, converting the value if neccessary.<T> T
Get the value of a field, converting it if necessary.Get all values of the entity as a map.default boolean
hasKey()
boolean
isFieldChanged
(String field) Returns true if the current field value differs from initially loaded value in current transaction.boolean
isFieldTouched
(String field) Deprecated.boolean
Determine whether the entity is updatable.void
joinTx()
Joins the current transaction.default Boolean
requireBool
(String field) Shortcut forrequireValue(field, Boolean.class)
.default org.joda.time.LocalDate
requireDate
(String field) Shortcut forrequireValue(field, LocalDate.class)
.default org.joda.time.DateTime
requireDateTime
(String field) Shortcut forrequireValue(field, DateTime.class)
.default BigDecimal
requireDecimal
(String field) Shortcut forrequireValue(field, BigDecimal.class)
.default Double
requireDouble
(String field) Shortcut forrequireValue(field, Double.class)
.default Float
requireFloat
(String field) Shortcut forrequireValue(field, Float.class)
.default Integer
requireInt
(String field) Shortcut forrequireValue(field, Integer.class)
.default PrimaryKey
Get the primary key of this already persisted entity.default Long
requireLong
(String field) Shortcut forrequireValue(field, Long.class)
.default @Nullable Short
requireShort
(String field) Shortcut forrequireValue(field, Short.class)
.default String
requireString
(String field) Shortcut forrequireValue(field, String.class)
.default org.joda.time.LocalTime
requireTime
(String field) Shortcut forrequireValue(field, LocalTime.class)
.default TypedValue
<?> requireValue
(FieldModel field) Get the value and (nominal) type of a field.default <T> T
requireValue
(FieldModel fieldModel, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.default <T> T
requireValue
(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> T
requireValue
(String field, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.default <T> T
requireValue
(String field, Class<T> targetType) Get the value of a field, converting it if necessary.Resolve a relation.void
setAutoFieldsDisabled
(boolean disabled) Disables fields listedEntityAutoField
(like version, update-user and -timestamps).void
Set the value of a field.default CompleteEntityValidationResult
validate()
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:
getKey
in 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:
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
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. Throwsch.tocco.nice2.security.UnauthorizedException
If 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 ofnull
is 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 ofnull
is 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:
-
isFieldTouched
Deprecated.- Throws:
ModelException
-
getTouchedFields
Deprecated.usegetChangedFields()
(String)} -
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).
-
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 returnsnull
if none.- Parameters:
relName
- The relation name.- Returns:
- The single related entity loaded eagerly, or
null
if 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:
getModel
in 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.
-
getChangedFields()
(String)}