Class Model

java.lang.Object
ch.autumo.commons.database.Model
All Implemented Interfaces:
Entity, Serializable

public abstract class Model extends Object implements Entity
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 Details

    • LOG

      protected static final org.slf4j.Logger LOG
      Logger.
    • ID_UNASSIGNED

      public static final int ID_UNASSIGNED
      Unassigned id; a model has been created but or loaded from database that has no id.
      See Also:
    • ID_INVALID

      public static final int ID_INVALID
      Invalid ID; used when an object cannot be read or stored.
      See Also:
    • ID_M2M_PSEUDO

      public static final int ID_M2M_PSEUDO
      Pseudo 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.
      Specified by:
      getId in interface Entity
      Returns:
      id
    • setId

      public void setId(int id)
      Set the ID. Should not be called by your code!
      Specified by:
      setId in interface Entity
      Parameters:
      id - id
    • isNullable

      public boolean isNullable(String beanPropertyName)
      Is the field specified by the bean property name nullable?
      Parameters:
      beanPropertyName - bean property name
      Returns:
      true if so
    • isUnique

      public boolean isUnique(String beanPropertyName)
      Is the field specified by the bean property name unique?
      Parameters:
      beanPropertyName - bean property name
      Returns:
      true if so
    • get

      public String get(String fieldName)
      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

      public Integer 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

      public Integer save(Connection conn) throws SQLException
      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

      public void update(Connection conn) throws SQLException
      Update this entity bean in database.
      Parameters:
      conn - global connection
      Throws:
      SQLException - SQL Exception
    • delete

      public void delete() throws Exception
      Delete this entity bean!
      Throws:
      Exception - exception
    • delete

      public void delete(Connection conn) throws SQLException
      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

      public Model getAssociatedReference(Class<?> referenceClass)
      Get associated (parent) entity.
      Parameters:
      referenceClass - referenced entity class
      Returns:
      referenced entity if any or null
    • getAssociatedReference

      public Model getAssociatedReference(String referenceBeanPropertyName)
      Get associated (parent) entity.
      Parameters:
      referenceBeanPropertyName - referenced entity bean property name
      Returns:
      referenced entity if any or null
    • listReferences

      public List<Model> listReferences(Class<?> referenceClass)
      List referenced entities of the referenced class type.
      Parameters:
      referenceClass - reference class
      Returns:
      list of entities that reference to this entity
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • clone

      public Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • serialize

      public String serialize(Model entity) throws com.fasterxml.jackson.core.JsonProcessingException
      Serialize this object to JSON.
      Parameters:
      entity - entity
      Returns:
      JSON string
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - JSON processing exception
    • deserialize

      public Model deserialize(String json) throws com.fasterxml.jackson.core.JsonProcessingException
      De-serialize a JSON string to an entity.
      Parameters:
      json - JSON string
      Returns:
      entity
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - JSON processing exception
    • getTableName

      public String getTableName()
      Returns name of corresponding table.
      Returns:
      name of corresponding table.
    • read

      public static Model read(Class<?> entity, int id)
      Read an entity with given ID.
      Parameters:
      entity - entity bean class
      id - ID
      Returns:
      entity
    • exists

      public static boolean exists(Class<?> entity, int id)
      Checks if the entity exists in the DB. If you further process an existing entity, better call read(Class, int).
      Parameters:
      entity - entity bean class
      id - ID
      Returns:
      true, if it exists
    • listAll

      public static List<Model> listAll(Class<?> entity)
      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

      public static List<Model> where(Class<?> entity, String condition, Object value, int amount)
      List all entities of the given entity bean with specific condition, e.g. 'age >= ?'.
      Parameters:
      entity - entity bean class
      condition - SQL condition
      value - value for the condition with one argument
      amount - max records to return
      Returns:
      entities
    • where

      public static List<Model> where(Class<?> entity, String condition, Object value)
      List all entities of the given entity bean with specific condition, e.g. 'age >= ?'.
      Parameters:
      entity - entity bean class
      condition - SQL condition
      value - value for the condition with one argument
      Returns:
      entities
    • where

      public static List<Model> where(Class<?> entity, String condition, Object[] values, int amount)
      List all entities of the given entity bean with specific condition, e.g. 'age >= ?, gender = ?'.
      Parameters:
      entity - entity bean class
      condition - SQL condition
      values - values for the condition
      amount - max records to return
      Returns:
      entities
    • where

      public static List<Model> where(Class<?> entity, String condition, Object[] values)
      List all entities of the given entity bean with specific condition, e.g. 'age >= ?, gender = ?'.
      Parameters:
      entity - entity bean class
      condition - SQL condition
      values - values for the condition
      Returns:
      entities
    • findFirst

      public static Model findFirst(Class<?> entity, String condition, Object value)
      Get first entity of the given entity bean with specific condition, e.g. 'age >= ?'.
      Parameters:
      entity - entity bean class
      condition - SQL condition
      value - value for the condition with one argument
      Returns:
      entity
    • findFirst

      public static Model findFirst(Class<?> entity, String condition, Object[] values)
      Get first entity of the given entity bean with specific condition, e.g. 'age >= ?'.
      Parameters:
      entity - entity bean class
      condition - SQL condition
      values - values for the condition
      Returns:
      entity
    • getDisplayField

      public String getDisplayField()
      Default display field.
      Returns:
      display field
    • getDisplayValue

      public String getDisplayValue()
      Get display value of bean.
      Returns:
      display value
    • getForeignReferences

      public Map<String,Class<?>> getForeignReferences()
      Get reference entity map (all referenced entities). The (PLANT-generated) mapping is:
      <databaseFieldName> -> <Class>
      Returns:
      reference entity map
    • modelClass

      public abstract Class<?> modelClass()
      Get model base class.
      Returns:
      model base class