Class BaseService<T extends BaseEntity>

  • Direct Known Subclasses:
    BaseCodeAndDescriptionService

    public class BaseService<T extends BaseEntity>
    extends java.lang.Object
    Author:
    Collin Alpert

    Class that provides base functionality for all service classes. Every service class must extend this class.

    • Constructor Summary

      Constructors 
      Constructor Description
      BaseService​(java.lang.Class<T> type)
      Constructor for the base class of all services.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void create​(T instance)
      Creates this Java entity on the database.
      void delete​(long id)
      Deletes a row by an id.
      void delete​(T instance)
      Deletes the corresponding row on the database.
      java.util.List<T> getAll()  
      java.util.Optional<T> getById​(long id)  
      protected Query<T> getMultiple​(SqlPredicate<T> predicate)
      Retrieves list of entities which match the predicate.
      protected java.util.Optional<T> getSingle​(SqlPredicate<T> predicate)
      Retrieves a single entity which matches the predicate.
      protected Query<T> selectQuery()  
      protected Query<T> subSelectQuery​(Query<T> subSelect)
      Creates a DQL statement which contains a sub select.
      void update​(T instance)
      Updates this entity's row on the database.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BaseService

        public BaseService​(java.lang.Class<T> type)
        Constructor for the base class of all services. It is possible to create instances of it.
        Parameters:
        type - The entity class corresponding to this service class.
    • Method Detail

      • create

        public void create​(T instance)
                    throws java.sql.SQLException
        Creates this Java entity on the database.
        Parameters:
        instance - The instance to create on the database.
        Throws:
        java.sql.SQLException - if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type.
      • selectQuery

        protected Query<T> selectQuery()
        Returns:
        a Query object with which a DQL statement can be build, using operations like order by, limit etc.
      • subSelectQuery

        protected Query<T> subSelectQuery​(Query<T> subSelect)
        Creates a DQL statement which contains a sub select.
        Parameters:
        subSelect - The sub select to select from.
        Returns:
        A Query object containing the sub select.
      • getSingle

        protected java.util.Optional<T> getSingle​(SqlPredicate<T> predicate)
        Retrieves a single entity which matches the predicate. It is protected as it is only meant for use in methods of the respective service. This is to keep good programming practice and create descriptive methods for what kind of data you are getting.
        Parameters:
        predicate - The SqlPredicate to add constraints to a DQL query.
        Returns:
        An entity matching the result of the query.
      • getMultiple

        protected Query<T> getMultiple​(SqlPredicate<T> predicate)
        Retrieves list of entities which match the predicate. It is protected as it is only meant for use in methods of the respective service. This is to keep good programming practice and create descriptive methods for what kind of data you are getting.
        Parameters:
        predicate - The SqlPredicate to add constraints to a DQL statement.
        Returns:
        A list of entities matching the result of the query.
      • getById

        public java.util.Optional<T> getById​(long id)
        Parameters:
        id - The id of the desired entity.
        Returns:
        Gets an entity by its id.
      • getAll

        public java.util.List<T> getAll()
        Returns:
        All entities in this table.
      • update

        public void update​(T instance)
                    throws java.sql.SQLException
        Updates this entity's row on the database.
        Parameters:
        instance - The instance to update on the database.
        Throws:
        java.sql.SQLException - if the query cannot be executed due to database constraints i.e. non-existing default value for field or an incorrect data type.
      • delete

        public void delete​(T instance)
        Deletes the corresponding row on the database.
        Parameters:
        instance - The instance to delete on the database.
      • delete

        public void delete​(long id)
        Deletes a row by an id.
        Parameters:
        id - The row with this id to delete.