4.2.1.1. Base Entity Classes

The base entity classes and interfaces are described in detail in this section.

EntityClasses
Figure 9. Base Entity Classes
  • Instance – declares the basic methods for working with objects of application domain:

    • getting the global unique identifier (UUID) of the entity

    • getting references to the object meta-class

    • generating the instance name

    • reading/writing attribute values by name

    • adding listeners receiving notifications about attribute changes

  • Entity – extends Instance with entity identifier (which is not necessarily equal to the UUID); at the same time Entity does not define the type of the identifier leaving this option to descendants.

  • AbstractInstance – implements the logic of working with attribute change listeners.

    Warning

    AbstractInstance stores the listeners in WeakReference, and if there are no external references to the added listener, it will be immediately destroyed by garbage collector. Normally, attribute change listeners are visual components and UI datasources that are always referenced by other objects, so there is no problem with listeners dropout. However, if a listener is created by application code and no objects refer to it in a natural way, it is necessary to save it in a certain object field apart from just adding it to Instance.

  • AbstractNotPersistentEntity – base class of non-persistent entities with UUID identifier.

  • BaseEntity – base class of persistent entities; declares methods for working with information about when and by whom the entity instance was created in the database.

  • BaseGenericIdEntity – implements BaseEntity with added JPA annotations without specifying the type of the identifier (i.e. the primary key) of the entity.

  • BaseUuidEntity – extends BaseGenericIdEntity and sets the id identification attribute of the UUID type.

  • BaseLongIdEntity – extends BaseGenericIdEntity and sets the id identification attribute of the Long type.

  • BaseIntegerIdEntity – extends BaseGenericIdEntity and sets the id identification attribute of the Integer type.

  • BaseStringIdEntity – extends BaseGenericIdEntity and sets only the type of the identifier - String. A specific entity class, extended from BaseStringIdEntity, must have a String-type identifier attribute with the @Id JPA annotation.

  • Versioned – interface for entities supporting optimistic locking.

  • Updatable – interface for entities which require to keep the information about when and by whom the instance was last changed.

  • SoftDelete – interface for entities supporting soft deletion.

  • StandardEntity – the most commonly used base class of persistent entities that implements the interfaces given above.

When creating entity classes it is recommended to choose a base class according to the following rules:

  • If an entity is not stored in the database, inherit it from AbstractNotPersistentEntity.

  • If an entity is embedded, inherit it from EmbeddableEntity.

  • If an entity is only created in DB, never changes and does not need soft deletion, inherit it from BaseUuidEntity.

  • If an entity behaves in a standard way: changes in the database, requires optimistic locking and soft deletion − inherit it from StandardEntity.

  • Otherwise inherit the entity from BaseUuidEntity and implement Versioned, Updatable, SoftDelete interfaces if required.

  • For some entities, it is desirable to use integer or string primary keys. In this case, inherit the entity from BaseLongIdEntity, BaseIntegerIdEntity or BaseStringIdEntity instead of BaseUuidEntity.