Package ch.autumo.commons.database
Class Model
java.lang.Object
ch.autumo.commons.database.Model
- All Implemented Interfaces:
Entity,Serializable
Model for all beetRoot entities. Basically an entity works fine
in the beetRoot CRUD views when only implementing the interface
Entity, but this model adds convenience functionalities
to entity beans:
- Select associated (parent) entity
- Select referenced entities
- List and find entities
- Save, update and delete
Note that all bean property names used within methods are
case sensitive!- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intInvalid ID; used when an object cannot be read or stored.static final intPseudo ID of an a stored model that is a many-to-many-relation-model-static final intUnassigned id; a model has been created but or loaded from database that has no id.protected static final org.slf4j.LoggerLogger. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()voiddelete()Delete this entity bean!voiddelete(Connection conn) Delete this entity bean!deserialize(String json) De-serialize a JSON string to an entity.booleanstatic booleanChecks if the entity exists in the DB.static ModelGet first entity of the given entity bean with specific condition, e.g.static ModelGet first entity of the given entity bean with specific condition, e.g.Get a value from this entity except the id.getAssociatedReference(Class<?> referenceClass) Get associated (parent) entity.getAssociatedReference(String referenceBeanPropertyName) Get associated (parent) entity.Default display field.Get display value of bean.Get reference entity map (all referenced entities).intgetId()Get the ID.Returns name of corresponding table.booleanisNullable(String beanPropertyName) Is the field specified by the bean property name nullable?booleanisStored()Is this entity bean stored in database?booleanIs the field specified by the bean property name unique?List all entities of the given entity bean.listReferences(Class<?> referenceClass) List referenced entities of the referenced class type.abstract Class<?> Get model base class.static ModelRead an entity with given ID.save()Save this entity bean to database.save(Connection conn) Save this entity bean to database.Serialize this object to JSON.voidsetId(int id) Set the ID.voidsetStored(boolean isStored) Set stored state.toString()voidupdate()Update this entity bean in database.voidupdate(Connection conn) Update this entity bean in database.List all entities of the given entity bean with specific condition, e.g.List all entities of the given entity bean with specific condition, e.g.List all entities of the given entity bean with specific condition, e.g.List all entities of the given entity bean with specific condition, e.g.
-
Field Details
-
LOG
protected static final org.slf4j.Logger LOGLogger. -
ID_UNASSIGNED
public static final int ID_UNASSIGNEDUnassigned id; a model has been created but or loaded from database that has no id.- See Also:
-
ID_INVALID
public static final int ID_INVALIDInvalid ID; used when an object cannot be read or stored.- See Also:
-
ID_M2M_PSEUDO
public static final int ID_M2M_PSEUDOPseudo ID of an a stored model that is a many-to-many-relation-model-- See Also:
-
-
Constructor Details
-
Model
public Model()Default constructor.
-
-
Method Details
-
getId
public int getId()Get the ID. Currently only number IDs are provided. If you need another type of ID, add it as a separate database field. -
setId
public void setId(int id) Set the ID. Should not be called by your code! -
isNullable
Is the field specified by the bean property name nullable?- Parameters:
beanPropertyName- bean property name- Returns:
- true if so
-
isUnique
Is the field specified by the bean property name unique?- Parameters:
beanPropertyName- bean property name- Returns:
- true if so
-
get
Get a value from this entity except the id. You even can use DB field name such as 'user_id' or 'max_asset_value'.- Parameters:
fieldName- field name- Returns:
- bean value
-
save
Save this entity bean to database.- Returns:
- generated id or -1 if entity couldn't be saved or the pseudo id -2 for many-to-many relation tables that have no id
-
save
Save this entity bean to database.- Parameters:
conn- connection- Returns:
- generated id or -1 if entity couldn't be saved or the pseudo id -2 for many-to-many relation tables that have no id
- Throws:
SQLException- SQL Exception
-
update
public void update()Update this entity bean in database. -
update
Update this entity bean in database.- Parameters:
conn- global connection- Throws:
SQLException- SQL Exception
-
delete
Delete this entity bean!- Throws:
Exception- exception
-
delete
Delete this entity bean!- Parameters:
conn- global connection- Throws:
SQLException- SQL Exception
-
setStored
public void setStored(boolean isStored) Set stored state.- Parameters:
isStored- true if this bean us stored in database
-
isStored
public boolean isStored()Is this entity bean stored in database?- Returns:
- true if so
-
getAssociatedReference
Get associated (parent) entity.- Parameters:
referenceClass- referenced entity class- Returns:
- referenced entity if any or null
-
getAssociatedReference
Get associated (parent) entity.- Parameters:
referenceBeanPropertyName- referenced entity bean property name- Returns:
- referenced entity if any or null
-
listReferences
List referenced entities of the referenced class type.- Parameters:
referenceClass- reference class- Returns:
- list of entities that reference to this entity
-
toString
-
equals
-
clone
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
serialize
Serialize this object to JSON.- Parameters:
entity- entity- Returns:
- JSON string
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- JSON processing exception
-
deserialize
De-serialize a JSON string to an entity.- Parameters:
json- JSON string- Returns:
- entity
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- JSON processing exception
-
getTableName
Returns name of corresponding table.- Returns:
- name of corresponding table.
-
read
Read an entity with given ID.- Parameters:
entity- entity bean classid- ID- Returns:
- entity
-
exists
Checks if the entity exists in the DB. If you further process an existing entity, better callread(Class, int).- Parameters:
entity- entity bean classid- ID- Returns:
- true, if it exists
-
listAll
List all entities of the given entity bean. Be aware: This doesn't limit the amount of records selected in database!- Parameters:
entity- entity bean class- Returns:
- entities
-
where
List all entities of the given entity bean with specific condition, e.g. 'age >= ?'.- Parameters:
entity- entity bean classcondition- SQL conditionvalue- value for the condition with one argumentamount- max records to return- Returns:
- entities
-
where
List all entities of the given entity bean with specific condition, e.g. 'age >= ?'.- Parameters:
entity- entity bean classcondition- SQL conditionvalue- value for the condition with one argument- Returns:
- entities
-
where
List all entities of the given entity bean with specific condition, e.g. 'age >= ?, gender = ?'.- Parameters:
entity- entity bean classcondition- SQL conditionvalues- values for the conditionamount- max records to return- Returns:
- entities
-
where
List all entities of the given entity bean with specific condition, e.g. 'age >= ?, gender = ?'.- Parameters:
entity- entity bean classcondition- SQL conditionvalues- values for the condition- Returns:
- entities
-
findFirst
Get first entity of the given entity bean with specific condition, e.g. 'age >= ?'.- Parameters:
entity- entity bean classcondition- SQL conditionvalue- value for the condition with one argument- Returns:
- entity
-
findFirst
Get first entity of the given entity bean with specific condition, e.g. 'age >= ?'.- Parameters:
entity- entity bean classcondition- SQL conditionvalues- values for the condition- Returns:
- entity
-
getDisplayField
Default display field.- Returns:
- display field
-
getDisplayValue
Get display value of bean.- Returns:
- display value
-
getForeignReferences
Get reference entity map (all referenced entities). The (PLANT-generated) mapping is:
<databaseFieldName> -> <Class>- Returns:
- reference entity map
-
modelClass
Get model base class.- Returns:
- model base class
-