-
- All Superinterfaces:
Comparable<Entity>
public interface Entity extends Comparable<Entity>
Represents a row in a table or query. Helper class for working with Entity instances and related classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
Entity.Builder
A builder forEntity
instances.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static Entity.Builder
builder(Key key)
<T extends Entity>
TcastTo(Class<T> entityClass)
Casts this entity to the given type.static <T extends Entity>
List<T>castTo(Class<T> entityClass, List<Entity> entities)
Casts the given entities to the given type.Entity
clearPrimaryKey()
Clears the primary key values from this entity, current as well as original values if anyboolean
columnValuesEqual(Entity entity)
Compares all column based values in the given entity to the values in this entity instance.boolean
contains(Attribute<?> attribute)
Returns true if this Entity contains a value for the given attribute, that value can be null.Entity
copy()
Copies this entity.static List<Entity>
copy(List<? extends Entity> entities)
Copies the given entities.Entity.Builder
copyBuilder()
Returns a newEntity.Builder
instance initialized with the values and original values from this entity.Entity
deepCopy()
Copies this entity, with new copied instances of all foreign key value entities.static List<Entity>
deepCopy(List<? extends Entity> entities)
Deep copies the given entities, with new copied instances of all foreign key value entities.EntityDefinition
definition()
static Entity
entity(Key key)
Set<Map.Entry<Attribute<?>,Object>>
entrySet()
Returns an unmodifiable view of the entries in this Entity, note that attributes based on derived values are not included.<T> T
get(Attribute<T> attribute)
Returns the value associated withattribute
.static <T> Collection<T>
get(Attribute<T> attribute, Collection<Entity> entities)
Returns the non-null values associated with the given attribute from the given entities.static Collection<Entity>
getByValue(Map<Attribute<?>,Object> values, Collection<Entity> entities)
Finds entities according to attribute valuesstatic <T> Collection<T>
getDistinct(Attribute<T> attribute, Collection<Entity> entities)
Returns the distinct non-null values ofattribute
from the given entities.static <T> Collection<T>
getDistinctIncludingNull(Attribute<T> attribute, Collection<Entity> entities)
Returns the distinct values ofattribute
from the given entities, including null.static <T> Collection<T>
getIncludingNull(Attribute<T> attribute, Collection<Entity> entities)
Returns the values associated with the given attribute from the given entities.static <T extends Entity>
List<T>getModified(Collection<T> entities)
Returns the entities which have been modifiedstatic Collection<Attribute<?>>
getModifiedColumnAttributes(Entity entity, Entity comparison)
Returns all updatableAttribute
s which value is missing or the original value differs from the one in the comparison entity, returns an empty Collection if all ofentity
s original values match the values found incomparison
.<T> Optional<T>
getOptional(Attribute<T> attribute)
Returns the value associated withattribute
, wrapped in anOptional
.<T> T
getOriginal(Attribute<T> attribute)
Returns the original value associated withattribute
.static Collection<Key>
getOriginalPrimaryKeys(Collection<? extends Entity> entities)
Returns the primary keys of the given entities with their original values.static List<Key>
getPrimaryKeys(Collection<? extends Entity> entities)
Returns the primary keys of the given entities.static Set<Key>
getReferencedKeys(Collection<? extends Entity> entities, ForeignKey foreignKey)
Returns the keys referenced by the given foreign keystatic List<List<String>>
getStringValueList(List<Attribute<?>> attributes, List<Entity> entities)
Creates a two-dimensional list containing the values of the given attributes for the given entities in string format.static <T> Collection<T>
getValues(Collection<Key> keys)
Retrieves the values of the given keys, assuming they are single column keys.static boolean
isKeyModified(Collection<Entity> entities)
Checks if any of the primary keys of the given entities is modifiedboolean
isLoaded(ForeignKey foreignKey)
Returns true if the given foreign key references a non-null entity and that entity instance has been loadedboolean
isModified()
Returns true if one or more writable attributes have been modified from their initial value, read only and non-updatable attributes are excluded unless they are transient.boolean
isModified(Attribute<?> attribute)
Returns true if the value associated with the given attribute has been modified since first set, note that this does not apply to attributes based on derived values.boolean
isNew()
Returns true if this entity has a null primary key or a null original primary key, which is the best guess about an entity being new, as in, not existing in a database.boolean
isNotNull(Attribute<?> attribute)
Returns true if this Entity contains a non-null value mapped to the given attribute In case of foreign key attributes the value of the underlying reference attribute is checked.boolean
isNull(Attribute<?> attribute)
Returns true if a null value is mapped to the given attribute or if no mapping is found.static <T> boolean
isValueMissingOrModified(Entity entity, Entity comparison, Attribute<T> attribute)
static LinkedHashMap<EntityType,List<Key>>
mapKeysToType(Collection<Key> keys)
Returns a LinkedHashMap containing the given entity keys mapped to their entityTypes, respecting the iteration order of the given collectionstatic Map<Key,Entity>
mapToPrimaryKey(Collection<Entity> entities)
Maps the given entities to their primary keystatic LinkedHashMap<EntityType,List<Entity>>
mapToType(Collection<? extends Entity> entities)
Returns a LinkedHashMap containing the given entities mapped to their entityTypes, respecting the iteration order of the given collectionstatic <T> LinkedHashMap<T,List<Entity>>
mapToValue(Attribute<T> attribute, Collection<Entity> entities)
Returns a LinkedHashMap containing the given entities mapped to the value ofattribute
, respecting the iteration order of the given collectionSet<Map.Entry<Attribute<?>,Object>>
originalEntrySet()
Key
originalPrimaryKey()
Returns the primary key of this entity, in its original state.Key
primaryKey()
Returns the primary key of this entity.<T> T
put(Attribute<T> attribute, T value)
Sets the value of the given attribute, returning the old value if anystatic <T> Map<Key,T>
put(Attribute<T> attribute, T value, Collection<Entity> entities)
Sets the value of the given attribute to the given value in the given entitiesstatic void
put(Entity destination, Entity source)
Puts all the values from the source entity into the destination entity.Entity
referencedEntity(ForeignKey foreignKey)
Returns the Entity instance referenced by the given foreign key attribute.Key
referencedKey(ForeignKey foreignKey)
Returns the primary key of the entity referenced by the givenAttribute
, if the reference is null this method returns null.<T> T
remove(Attribute<T> attribute)
Removes the given value from this Entity along with the original value if any.void
revert(Attribute<?> attribute)
Reverts the value associated with the given attribute to its original value.void
revertAll()
Reverts all value modifications that have been made.void
save(Attribute<?> attribute)
Saves the value associated with the given attribute, that is, removes the original value.void
saveAll()
Saves all the value modifications that have been made.Map<Attribute<?>,Object>
setAs(Entity entity)
After a call to this method this Entity contains the same values and original values as the source entity.<T> String
toString(Attribute<T> attribute)
This method returns a String representation of the value associated with the given attribute, if the associated property has a format it is used.EntityType
type()
static boolean
valuesEqual(Entity entityOne, Entity entityTwo)
Returns true if all attribute values are equal in the given entities.static boolean
valuesEqual(Entity entityOne, Entity entityTwo, Attribute<?>... attributes)
Returns true if the values of the given attributes are equal in the given entities.-
Methods inherited from interface java.lang.Comparable
compareTo
-
-
-
-
Method Detail
-
type
EntityType type()
- Returns:
- the entity type
-
definition
EntityDefinition definition()
- Returns:
- the entity definition
-
put
<T> T put(Attribute<T> attribute, T value)
Sets the value of the given attribute, returning the old value if any- Type Parameters:
T
- the value type- Parameters:
attribute
- the attributevalue
- the value- Returns:
- the previous value
-
get
<T> T get(Attribute<T> attribute)
Returns the value associated withattribute
.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute for which to retrieve the value- Returns:
- the value of the given attribute
-
getOptional
<T> Optional<T> getOptional(Attribute<T> attribute)
Returns the value associated withattribute
, wrapped in anOptional
.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute for which to retrieve the value- Returns:
- the value of the given attribute, wrapped in an
Optional
-
getOriginal
<T> T getOriginal(Attribute<T> attribute)
Returns the original value associated withattribute
. If the value has not been modified the current value is returned.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute for which to retrieve the original value- Returns:
- the original value of the given attribute
-
toString
<T> String toString(Attribute<T> attribute)
This method returns a String representation of the value associated with the given attribute, if the associated property has a format it is used.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute for which to retrieve the value- Returns:
- a String representation of the value associated with
attribute
-
revert
void revert(Attribute<?> attribute)
Reverts the value associated with the given attribute to its original value. If the value has not been modified then calling this method has no effect.- Parameters:
attribute
- the attribute for which to revert the value
-
revertAll
void revertAll()
Reverts all value modifications that have been made. This entity will be unmodified after a call to this method. If no modifications have been made then calling this method has no effect.
-
save
void save(Attribute<?> attribute)
Saves the value associated with the given attribute, that is, removes the original value. If no original value exists calling this method has no effect.- Parameters:
attribute
- the attribute for which to save the value
-
saveAll
void saveAll()
Saves all the value modifications that have been made. This entity will be unmodified after a call to this method.- See Also:
isModified()
-
remove
<T> T remove(Attribute<T> attribute)
Removes the given value from this Entity along with the original value if any. If no value is mapped to the given attribute, this method has no effect.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute to remove- Returns:
- the previous value mapped to the given attribute
-
isNull
boolean isNull(Attribute<?> attribute)
Returns true if a null value is mapped to the given attribute or if no mapping is found. In case of foreign key attributes the value of the underlying reference attribute is checked.- Parameters:
attribute
- the attribute- Returns:
- true if the value mapped to the given attribute is null or no value is mapped
-
isNotNull
boolean isNotNull(Attribute<?> attribute)
Returns true if this Entity contains a non-null value mapped to the given attribute In case of foreign key attributes the value of the underlying reference attribute is checked.- Parameters:
attribute
- the attribute- Returns:
- true if a non-null value is mapped to the given attribute
-
contains
boolean contains(Attribute<?> attribute)
Returns true if this Entity contains a value for the given attribute, that value can be null.- Parameters:
attribute
- the attribute- Returns:
- true if a value is mapped to this attribute
-
referencedEntity
Entity referencedEntity(ForeignKey foreignKey)
Returns the Entity instance referenced by the given foreign key attribute. If the underlying reference attribute contains a value, that is, a foreign key value exists but the actual referenced entity has not been loaded, an "empty" entity is returned, containing only the primary key value. Null is returned only if the actual reference attribute is null.- Parameters:
foreignKey
- the foreign key for which to retrieve the value- Returns:
- the value of
foreignKey
, assuming it is an Entity - Throws:
IllegalArgumentException
- if the attribute is not a foreign key attribute- See Also:
isLoaded(ForeignKey)
-
referencedKey
Key referencedKey(ForeignKey foreignKey)
Returns the primary key of the entity referenced by the givenAttribute
, if the reference is null this method returns null.- Parameters:
foreignKey
- the foreign key for which to retrieve the underlyingKey
- Returns:
- the primary key of the underlying entity, null if no entity is referenced
-
isModified
boolean isModified(Attribute<?> attribute)
Returns true if the value associated with the given attribute has been modified since first set, note that this does not apply to attributes based on derived values.- Parameters:
attribute
- the attribute- Returns:
- true if the value associated with the given attribute has been modified
-
isModified
boolean isModified()
Returns true if one or more writable attributes have been modified from their initial value, read only and non-updatable attributes are excluded unless they are transient.- Returns:
- true if one or more attributes have been modified since the entity was initialized
-
isNew
boolean isNew()
Returns true if this entity has a null primary key or a null original primary key, which is the best guess about an entity being new, as in, not existing in a database.- Returns:
- true if this entity has not been persisted
-
clearPrimaryKey
Entity clearPrimaryKey()
Clears the primary key values from this entity, current as well as original values if any- Returns:
- this Entity instance
-
columnValuesEqual
boolean columnValuesEqual(Entity entity)
Compares all column based values in the given entity to the values in this entity instance. Returns true if these two entities contain values for the same columns and all the values are equal.- Parameters:
entity
- the entity to compare to- Returns:
- true if all the column based values in this entity instance are present and equal to the values in the given entity
- Throws:
IllegalArgumentException
- in case the entity is not of the same type
-
setAs
Map<Attribute<?>,Object> setAs(Entity entity)
After a call to this method this Entity contains the same values and original values as the source entity. A null argument to this method clears this entity instance of all values and original values.- Parameters:
entity
- the entity to copy or null for clearing all values in this instance- Returns:
- the affected attributes and their previous values, that is, attributes which values changed
- Throws:
IllegalArgumentException
- in case the entity is not of the same type
-
copy
Entity copy()
Copies this entity.- Returns:
- a copy of this entity
-
copyBuilder
Entity.Builder copyBuilder()
Returns a newEntity.Builder
instance initialized with the values and original values from this entity.- Returns:
- a
Entity.Builder
instance.
-
deepCopy
Entity deepCopy()
Copies this entity, with new copied instances of all foreign key value entities.- Returns:
- a deep copy of this entity
-
castTo
<T extends Entity> T castTo(Class<T> entityClass)
Casts this entity to the given type.- Type Parameters:
T
- the entity class type- Parameters:
entityClass
- the entity class to cast to- Returns:
- a typed entity
- Throws:
IllegalArgumentException
- in case the given class has not been associated with the underlyingEntityType
.
-
isLoaded
boolean isLoaded(ForeignKey foreignKey)
Returns true if the given foreign key references a non-null entity and that entity instance has been loaded- Parameters:
foreignKey
- the attribute- Returns:
- true if the referenced entity has been loaded
-
primaryKey
Key primaryKey()
Returns the primary key of this entity. If the entity has no primary key attribute defined, this key contains no values.- Returns:
- the primary key of this entity
-
originalPrimaryKey
Key originalPrimaryKey()
Returns the primary key of this entity, in its original state. If the entity has no primary key attributes defined, this key contains no values.- Returns:
- the primary key of this entity in its original state
-
entrySet
Set<Map.Entry<Attribute<?>,Object>> entrySet()
Returns an unmodifiable view of the entries in this Entity, note that attributes based on derived values are not included.- Returns:
- an unmodifiable view of the entries in this Entity
-
originalEntrySet
Set<Map.Entry<Attribute<?>,Object>> originalEntrySet()
- Returns:
- an unmodifiable view of the original entries values in this Entity, that is, the original values of attributes that have been modified
-
entity
static Entity entity(Key key)
- Parameters:
key
- the key- Returns:
- an Entity instance based on the given key
-
builder
static Entity.Builder builder(Key key)
- Parameters:
key
- the key- Returns:
- a builder instance based on the given key
-
isKeyModified
static boolean isKeyModified(Collection<Entity> entities)
Checks if any of the primary keys of the given entities is modified- Parameters:
entities
- the entities to check- Returns:
- true if any of the given entities has a modified primary key
-
getModified
static <T extends Entity> List<T> getModified(Collection<T> entities)
Returns the entities which have been modified- Type Parameters:
T
- the entity type- Parameters:
entities
- the entities- Returns:
- a List of entities that have been modified
- See Also:
isModified()
-
getModifiedColumnAttributes
static Collection<Attribute<?>> getModifiedColumnAttributes(Entity entity, Entity comparison)
Returns all updatableAttribute
s which value is missing or the original value differs from the one in the comparison entity, returns an empty Collection if all ofentity
s original values match the values found incomparison
. Note that only eagerly loaded blob values are included in this comparison.- Parameters:
entity
- the entity instance to checkcomparison
- the entity instance to compare with- Returns:
- the updatable column attributes which values differ from the ones in the comparison entity
- See Also:
BlobProperty.isEagerlyLoaded()
-
getPrimaryKeys
static List<Key> getPrimaryKeys(Collection<? extends Entity> entities)
Returns the primary keys of the given entities.- Parameters:
entities
- the entities- Returns:
- a List containing the primary keys of the given entities
-
getReferencedKeys
static Set<Key> getReferencedKeys(Collection<? extends Entity> entities, ForeignKey foreignKey)
Returns the keys referenced by the given foreign key- Parameters:
entities
- the entitiesforeignKey
- the foreign key- Returns:
- the keys referenced by the given foreign key
-
getOriginalPrimaryKeys
static Collection<Key> getOriginalPrimaryKeys(Collection<? extends Entity> entities)
Returns the primary keys of the given entities with their original values.- Parameters:
entities
- the entities- Returns:
- a Collection containing the primary keys of the given entities with their original values
-
getValues
static <T> Collection<T> getValues(Collection<Key> keys)
Retrieves the values of the given keys, assuming they are single column keys.- Type Parameters:
T
- the value type- Parameters:
keys
- the keys- Returns:
- the attribute values of the given keys
-
get
static <T> Collection<T> get(Attribute<T> attribute, Collection<Entity> entities)
Returns the non-null values associated with the given attribute from the given entities.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute which values to retrieveentities
- the entities from which to retrieve the attribute value- Returns:
- the non-null values of the given attribute from the given entities.
-
getIncludingNull
static <T> Collection<T> getIncludingNull(Attribute<T> attribute, Collection<Entity> entities)
Returns the values associated with the given attribute from the given entities.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute which values to retrieveentities
- the entities from which to retrieve the attribute value- Returns:
- the values of the given attributes from the given entities, including null values.
-
getDistinct
static <T> Collection<T> getDistinct(Attribute<T> attribute, Collection<Entity> entities)
Returns the distinct non-null values ofattribute
from the given entities.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute which values to retrieveentities
- the entities from which to retrieve the values- Returns:
- the distinct non-null values of the given attribute from the given entities.
-
getDistinctIncludingNull
static <T> Collection<T> getDistinctIncludingNull(Attribute<T> attribute, Collection<Entity> entities)
Returns the distinct values ofattribute
from the given entities, including null.- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute which values to retrieveentities
- the entities from which to retrieve the values- Returns:
- the distinct values of the given attribute from the given entities, may contain null.
-
put
static <T> Map<Key,T> put(Attribute<T> attribute, T value, Collection<Entity> entities)
Sets the value of the given attribute to the given value in the given entities- Type Parameters:
T
- the value type- Parameters:
attribute
- the attribute for which to set the valuevalue
- the valueentities
- the entities for which to set the value- Returns:
- the previous attribute values mapped to the primary key of the entity
-
put
static void put(Entity destination, Entity source)
Puts all the values from the source entity into the destination entity.- Parameters:
destination
- the destinationsource
- the source- Throws:
IllegalArgumentException
- in case the entities are not of the same type
-
deepCopy
static List<Entity> deepCopy(List<? extends Entity> entities)
Deep copies the given entities, with new copied instances of all foreign key value entities.- Parameters:
entities
- the entities to copy- Returns:
- a deep copy of the given entities
-
copy
static List<Entity> copy(List<? extends Entity> entities)
Copies the given entities.- Parameters:
entities
- the entities to copy- Returns:
- copies of the given entities, in the same order as they are received
-
castTo
static <T extends Entity> List<T> castTo(Class<T> entityClass, List<Entity> entities)
Casts the given entities to the given type.- Type Parameters:
T
- the entity class type- Parameters:
entityClass
- the entity class to cast toentities
- the entities- Returns:
- typed entities
- Throws:
IllegalArgumentException
- in case any of the entities is not of the given entity type
-
mapToPrimaryKey
static Map<Key,Entity> mapToPrimaryKey(Collection<Entity> entities)
Maps the given entities to their primary key- Parameters:
entities
- the entities to map- Returns:
- the mapped entities
-
mapToValue
static <T> LinkedHashMap<T,List<Entity>> mapToValue(Attribute<T> attribute, Collection<Entity> entities)
Returns a LinkedHashMap containing the given entities mapped to the value ofattribute
, respecting the iteration order of the given collection- Type Parameters:
T
- the key type- Parameters:
attribute
- the attribute which value should be used for mappingentities
- the entities to map by attribute value- Returns:
- a Map of entities mapped to attribute value
-
mapToType
static LinkedHashMap<EntityType,List<Entity>> mapToType(Collection<? extends Entity> entities)
Returns a LinkedHashMap containing the given entities mapped to their entityTypes, respecting the iteration order of the given collection- Parameters:
entities
- the entities to map by entityType- Returns:
- a Map of entities mapped to entityType
-
mapKeysToType
static LinkedHashMap<EntityType,List<Key>> mapKeysToType(Collection<Key> keys)
Returns a LinkedHashMap containing the given entity keys mapped to their entityTypes, respecting the iteration order of the given collection- Parameters:
keys
- the entity keys to map by entityType- Returns:
- a Map of entity keys mapped to entityType
-
getStringValueList
static List<List<String>> getStringValueList(List<Attribute<?>> attributes, List<Entity> entities)
Creates a two-dimensional list containing the values of the given attributes for the given entities in string format.- Parameters:
attributes
- the attributesentities
- the entities- Returns:
- the values of the given attributes from the given entities in a two-dimensional list
-
getByValue
static Collection<Entity> getByValue(Map<Attribute<?>,Object> values, Collection<Entity> entities)
Finds entities according to attribute values- Parameters:
values
- the attribute values to use as condition mapped to their respective attributesentities
- the entities to search- Returns:
- the entities having the exact same attribute values as in the given value map
-
valuesEqual
static boolean valuesEqual(Entity entityOne, Entity entityTwo)
Returns true if all attribute values are equal in the given entities.- Parameters:
entityOne
- the first entityentityTwo
- the second entity- Returns:
- true if the values of the given attributes are equal in the given entities
-
valuesEqual
static boolean valuesEqual(Entity entityOne, Entity entityTwo, Attribute<?>... attributes)
Returns true if the values of the given attributes are equal in the given entities.- Parameters:
entityOne
- the first entityentityTwo
- the second entityattributes
- the attributes which values to compare- Returns:
- true if the values of the given attributes are equal in the given entities
-
isValueMissingOrModified
static <T> boolean isValueMissingOrModified(Entity entity, Entity comparison, Attribute<T> attribute)
- Type Parameters:
T
- the attribute type- Parameters:
entity
- the entity instance to checkcomparison
- the entity instance to compare withattribute
- the attribute to check- Returns:
- true if the value is missing or the original value differs from the one in the comparison entity
-
-