Entity

Basic object and DB table representation

Entity is a database table representation.
However it can be created to keep data and querying over it, apart of database.

As table has an unique set of columns inside, so that Entity has unique set of Fields/Columns as well - implemented as interfaces.

Entity like this, may be used as exactly a regular Java object, but the way of storing an inner objects or fields is different - setters and getter are being exposed through interfaces and an internal Map keeps the values.



@SuppressWarnings("rawtypes")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public class Actor extends Table implements
       
ActorId<Actor, Integer>,
        FirstName<Actor, String>,
        LastName<Actor, String>,
        LastUpdate<Actor, LocalDateTime>,

        PrimaryKey<Actor, Integer, ActorId>
    {

   
@Override
   
public String getEntityName() {
       
return "ACTOR";
    }

   
@Override
   
public List<TableColumn> columns() {
       
return Arrays.asList(
               
new TableColumn(colActorId()).primaryKey().size(16),
               
new TableColumn(colFirstName()).size(45),
               
new TableColumn(colLastName()).size(45),
               
new TableColumn(colLastUpdate()).size(26).scale(6)
        );
    }

   
@Override
   
public Actor clone() {
      
return EntityDuplicator.cloneEntity(this);
    }

   
@Override
   
public Column<Actor,Integer,ActorId> colID() {
      
return colActorId();
    }

   
@Override
   
public int hashCode() {
      
return Objects.hashCode(getId());
    }

   
@Override
   
public boolean equals(Object obj) {
      
if (this == obj)return true;
      
if (obj == null)return false;
      
if (getClass() != obj.getClass())return false;
       PrimaryKey<?,?,?> other = (PrimaryKey<?,?,?>) obj;
      
return Objects.equals(getId(), other.getId());
    }

}

 

Each unique column has its own, unique interface, which is being implemented by enties.

This is how relations between tables are recreated into Daobab Entities.

For Java, entity is a column instance as well. 


Entity have to inherits particular column interface, otherwise reading or writing will be impossible.

Whole data is stored into a map only through interface method.

Entity used externally, provides a lot of useful methods:


- getters and chained setters

- deep cloning

- conversion to JSON 

- proper hashcode and equals

Moreover, if table has Primary Key, a PrimaryKey interface is added. A lot of useful methods are being exposed by this.


Entity also has a column() method, which returns all column interfaces.
It's not enough to add new interface, when a new column is added into a table. Remember to point to a new column into this method as well.


Entity may be protected via Optimistic Concurrency Control.

For special cases, Entity may be upgraded with ExtendedEntity interface.



Kotlin entity:

class Actor : Table(),
    ActorId<Actor, Int>,
    FirstName<Actor, String>,
    LastName<Actor, String>,
    LastUpdate<Actor, LocalDateTime>,

    PrimaryKey<Actor,Int,ActorId<*, Int>>
    {

   
override fun getEntityName() = "ACTOR"

   
override fun columns() =
        listOf(
          TableColumn(colActorId()).primaryKey().size(
16),
          TableColumn(colFirstName()).size(
45),
          TableColumn(colLastName()).size(
45),
          TableColumn(colLastUpdate()).size(
26).scale(6)
        )

   
override fun clone(): Actor  {
      
return EntityDuplicator.cloneEntity(this)
    }

   
override fun colID(): Column<Actor,Int,ActorId<*, Int>> {
      
return colActorId()
    }

   
override fun hashCode(): Int {
       
return Objects.hashCode(id)
    }

   
override fun equals(obj: Any?): Boolean {
       
if (this === obj) return true
        if
(obj == null) return false
        if
(javaClass != obj.javaClass) return false
        val
other: PrimaryKey<*, *, *> = obj as PrimaryKey<*, *, *>
       
return id == other.id
   
}


}

© Copyright 2018-2023 Elephant Software Klaudiusz Wojtkowiak 
e-mail: contact@daobab.io