Class BaseService<E extends BaseEntity>

java.lang.Object
com.github.collinalpert.java2db.services.BaseService<E>
Direct Known Subclasses:
AsyncBaseService, BaseCodeAndDescriptionService, BaseDeletableService

public class BaseService<E extends BaseEntity>
extends java.lang.Object
Class that provides base functionality for all service classes. Every service class must extend this class.
Author:
Collin Alpert
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.lang.String tableName
    Represents the table name of the entity this services corresponds to.
    protected java.lang.Class<E> type
    The generic type of this service.
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected BaseService()
    Constructor for the base class of all services.
  • Method Summary

    Modifier and Type Method Description
    boolean any()
    Checks if a table has at least one row.
    boolean any​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Checks if a value matching the condition exists in the table.
    long count()
    An overload of the count(SqlPredicate) method.
    long count​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Counts the rows that have an id (which should usually be every row) and match a certain condition.
    long create​(E instance)
    Creates this Java entity on the database and sets the newly created id for the entity.
    void create​(E... instances)
    Creates a variable amount of entities on the database.
    void create​(java.util.List<E> instances)
    Creates multiple entities on the database.
    PaginationResult<E> createPagination​(int entriesPerPage)
    Creates a pagination structure that splits the entire table into multiple pages.
    CacheablePaginationResult<E> createPagination​(int entriesPerPage, java.time.Duration cacheExpiration)
    Creates a pagination structure that splits the entire table into multiple pages.
    PaginationResult<E> createPagination​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate, int entriesPerPage)
    Creates a pagination structure that splits the result of a query into multiple pages.
    CacheablePaginationResult<E> createPagination​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate, int entriesPerPage, java.time.Duration cacheExpiration)
    Creates a cached pagination structure that splits the result of a query into multiple pages.
    protected EntityQuery<E> createQuery()  
    protected SingleEntityQuery<E> createSingleQuery()  
    void delete​(long id)
    Deletes a row by an id.
    void delete​(long... ids)
    Deletes multiple rows with the corresponding ids.
    void delete​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Deletes rows based on a condition.
    void delete​(E instance)
    Deletes the corresponding row on the database.
    void delete​(E... entities)
    Deletes multiple entities at once.
    void delete​(java.util.List<E> entities)
    Deletes a list of entities at once.
    java.util.List<E> getAll()  
    java.util.List<E> getAll​(OrderTypes sortingType, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?> orderBy)
    Gets all values from the table and orders them in the specified order by multiple columns in a coalescing manner.
    java.util.List<E> getAll​(OrderTypes sortingType, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>[] orderBy)
    Gets all values from the table and orders them in the specified order by multiple columns in a coalescing manner.
    java.util.List<E> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?> orderBy)
    Gets all values from the table and orders them in an ascending order.
    java.util.List<E> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>[] orderBy)
    Gets all values from the table and orders them in an ascending order by multiple columns in a coalescing manner.
    java.util.Optional<E> getById​(long id)  
    java.util.Optional<E> getFirst​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Retrieves a single entity which matches the predicate.
    EntityQuery<E> getMultiple​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Retrieves list of entities which match the predicate.
    SingleEntityQuery<E> getSingle​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Retrieves a single entity which matches the predicate.
    boolean hasDuplicates​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?> column)
    Checks if duplicate values exist for a specific column in a table.
    <T> T max​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column)
    Gets the maximum value of a column in a table.
    <T> T max​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column, com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Gets the maximum value of a column in a set of values filtered by a condition.
    <T> T min​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column)
    Gets the minimum value of a column in a table.
    <T> T min​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column, com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
    Gets the minimum value of a column in a set of values filtered by a condition.
    void truncateTable()
    Truncates the corresponding table on the database.
    <R> void update​(long entityId, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> column, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> newValueFunction)
    Updates a specific column based on the current column value in the database.
    <R> void update​(long entityId, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> column, R newValue)
    Updates a specific column for a single record in a table.
    <R> void update​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> condition, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> column, R newValue)
    Updates a specific column for records matching a condition in a table.
    void update​(E instance)
    Updates this entity's row on the database.
    void update​(E... instances)
    Variable argument version which behaves the same as the update(List) method.
    void update​(java.util.List<E> instances)
    Updates multiple entity's rows on the database.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • type

      protected final java.lang.Class<E extends BaseEntity> type
      The generic type of this service.
    • tableName

      protected final java.lang.String tableName
      Represents the table name of the entity this services corresponds to. It contains the table name with escaping backticks.
  • Constructor Details

    • BaseService

      protected BaseService()
      Constructor for the base class of all services. It is not possible to create instances of it.
  • Method Details

    • create

      public long create​(E instance) throws java.sql.SQLException
      Creates this Java entity on the database and sets the newly created id for the entity.
      Parameters:
      instance - The instance to create on the database.
      Returns:
      The id of the newly created record.
      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 or a foreign key constraint.
    • create

      public void create​(E... instances) throws java.sql.SQLException
      Creates a variable amount of entities on the database.
      Parameters:
      instances - The instances to create.
      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 or a foreign key constraint.
      See Also:
      create(List)
    • create

      public void create​(java.util.List<E> instances) throws java.sql.SQLException
      Creates multiple entities on the database. It is recommended to use this method instead of iterating over the list and calling a normal create(BaseEntity) on each entity separately.
      Parameters:
      instances - The list of entities 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 or a foreign key constraint.
    • count

      public long count()
      An overload of the count(SqlPredicate) method. It will count all the rows that have an id, since this should be a reasonable criteria. Use with caution, as this call can take quite a while.
      Returns:
      The amount of rows in this table.
    • count

      public long count​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Counts the rows that have an id (which should usually be every row) and match a certain condition. Note that COUNT operations usually are very slow.
      Parameters:
      predicate - The condition to test for.
      Returns:
      The number of rows matching the condition.
    • any

      public boolean any()
      Checks if a table has at least one row.
      Returns:
      True if at least one row exists in the table, false if not.
    • any

      public boolean any​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Checks if a value matching the condition exists in the table.
      Parameters:
      predicate - The condition to check for.
      Returns:
      True if the predicate matches one or more records, false if not.
    • max

      public <T> T max​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column)
      Gets the maximum value of a column in a table.
      Type Parameters:
      T - The generic type of the column. It is also the return type.
      Parameters:
      column - The column to get the maximum value of.
      Returns:
      The maximum value of the column.
    • max

      public <T> T max​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column, com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Gets the maximum value of a column in a set of values filtered by a condition.
      Type Parameters:
      T - The generic type of the column. It is also the return type.
      Parameters:
      column - The column to get the maximum value of.
      predicate - The predicate to filter by.
      Returns:
      The maximum value of the column.
    • min

      public <T> T min​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column)
      Gets the minimum value of a column in a table. Please note that this does not include null values.
      Type Parameters:
      T - The generic type of the column. It is also the return type.
      Parameters:
      column - The column to get the minimum value of.
      Returns:
      The minimum value of the column.
    • min

      public <T> T min​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​T> column, com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Gets the minimum value of a column in a set of values filtered by a condition. Please note that this does not include null values.
      Type Parameters:
      T - The generic type of the column. It is also the return type.
      Parameters:
      column - The column to get the minimum value of.
      predicate - The predicate to filter by.
      Returns:
      The minimum value of the column.
    • hasDuplicates

      public boolean hasDuplicates​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?> column)
      Checks if duplicate values exist for a specific column in a table.
      Parameters:
      column - The column to check for duplicate values for.
      Returns:
      True if there is at least one duplicate value in the specified column, false otherwise.
    • createQuery

      protected EntityQuery<E> createQuery()
      Returns:
      a EntityQuery object with which a DQL statement can be built, using operations like order by, limit etc. If you do not require a plain EntityQuery, please consider using the getSingle(SqlPredicate), getMultiple(SqlPredicate) or getAll() methods.
    • createSingleQuery

      protected SingleEntityQuery<E> createSingleQuery()
      Returns:
      a SingleEntityQuery object with which a DQL statement can be built, which returns a single record. If you do not require a plain SingleEntityQuery, please consider using the getSingle(SqlPredicate), getMultiple(SqlPredicate) or getAll() methods.
    • getFirst

      public java.util.Optional<E> getFirst​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Retrieves a single entity which matches the predicate.
      Parameters:
      predicate - The SqlPredicate to add constraints to a DQL query.
      Returns:
      An entity matching the result of the query.
    • getSingle

      public SingleEntityQuery<E> getSingle​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Retrieves a single entity which matches the predicate.
      Parameters:
      predicate - The SqlPredicate to add constraints to a DQL query.
      Returns:
      An entity matching the result of the query.
    • getMultiple

      public EntityQuery<E> getMultiple​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate)
      Retrieves list of entities which match the predicate.
      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<E> getById​(long id)
      Parameters:
      id - The id of the desired entity.
      Returns:
      Gets an entity by its id.
    • getAll

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

      public java.util.List<E> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?> orderBy)
      Gets all values from the table and orders them in an ascending order.
      Parameters:
      orderBy - The property to order by.
      Returns:
      A list of all records ordered by a specific property in an ascending order.
    • getAll

      public java.util.List<E> getAll​(com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>[] orderBy)
      Gets all values from the table and orders them in an ascending order by multiple columns in a coalescing manner.
      Parameters:
      orderBy - The properties to order by.
      Returns:
      A list of all records ordered by a specific property in an ascending order.
    • getAll

      public java.util.List<E> getAll​(OrderTypes sortingType, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?> orderBy)
      Gets all values from the table and orders them in the specified order by multiple columns in a coalescing manner.
      Parameters:
      orderBy - The property to order by.
      sortingType - The order direction. Can be either ascending or descending.
      Returns:
      A list of all records ordered by a specific property in the specified order.
    • getAll

      public java.util.List<E> getAll​(OrderTypes sortingType, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​?>[] orderBy)
      Gets all values from the table and orders them in the specified order by multiple columns in a coalescing manner.
      Parameters:
      orderBy - The properties to order by.
      sortingType - The order direction. Can be either ascending or descending.
      Returns:
      A list of all records ordered by a specific property in the specified order.
    • createPagination

      public PaginationResult<E> createPagination​(int entriesPerPage)
      Creates a pagination structure that splits the entire table into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called.
      Parameters:
      entriesPerPage - The number of entries to be displayed on each page.
      Returns:
      A pagination which splits up an entire table into multiple pages.
    • createPagination

      public PaginationResult<E> createPagination​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate, int entriesPerPage)
      Creates a pagination structure that splits the result of a query into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called. The predicate to filter by.
      Parameters:
      predicate - The predicate to filter by.
      entriesPerPage - The number of entries to be displayed on each page.
      Returns:
      A pagination which splits the result of a condition into multiple pages.
    • createPagination

      public CacheablePaginationResult<E> createPagination​(int entriesPerPage, java.time.Duration cacheExpiration)
      Creates a pagination structure that splits the entire table into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called. When pages are retrieved, they are stored in a cache, so they can be retrieved from there the next time they are requested. Note that this will work, until the specified cacheExpiration is up. After that, the next time a page is requested, it will be reloaded from the database.
      Parameters:
      entriesPerPage - The number of entries to be displayed on each page.
      cacheExpiration - The time a page is valid in the cache. If a page is requested and in the cache, but expired, it will be retrieved from the database again and re-stored in the cache.
      Returns:
      A pagination which displays a entire table
    • createPagination

      public CacheablePaginationResult<E> createPagination​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate, int entriesPerPage, java.time.Duration cacheExpiration)
      Creates a cached pagination structure that splits the result of a query into multiple pages. Note that the query to the database for each page is only executed when that specific page is requested and not when this method is called. When pages are retrieved, they are stored in a cache, so they can be retrieved from there the next time they are requested. Note that this will work, until the specified cacheExpiration is up. After that, the next time a page is requested, it will be reloaded from the database.
      Parameters:
      predicate - The predicate to filter by.
      entriesPerPage - The number of entries to be displayed on each page.
      cacheExpiration - The time a page is valid in the cache. If a page is requested and in the cache, but expired, it will be retrieved from the database again and re-stored in the cache.
      Returns:
      A pagination which allows the developer to retrieve specific pages from the result.
    • update

      public void update​(E 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.
    • update

      public void update​(E... instances) throws java.sql.SQLException
      Variable argument version which behaves the same as the update(List) method.
      Parameters:
      instances - The instances 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.
      See Also:
      update(List)
    • update

      public void update​(java.util.List<E> instances) throws java.sql.SQLException
      Updates multiple entity's rows on the database. This method only opens one connection to the database as oppose to when calling the update(BaseEntity) method in a for loop, which opens a new connection for every entity.
      Parameters:
      instances - The instances 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.
    • update

      public <R> void update​(long entityId, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> column, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> newValueFunction) throws java.sql.SQLException
      Updates a specific column based on the current column value in the database.
      Type Parameters:
      R - The type of the column to update.
      Parameters:
      entityId - The id of the row to update.
      column - The column to update.
      newValueFunction - The function to calculate the new value.
      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.
    • update

      public <R> void update​(long entityId, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> column, R newValue) throws java.sql.SQLException
      Updates a specific column for a single record in a table. This method is not affected by the DefaultIfNull annotation.
      Type Parameters:
      R - The data type of the column to update. It must be the same as the data type of the new value.
      Parameters:
      entityId - The id of the record.
      column - The column to update.
      newValue - The new value of the column.
      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.
    • update

      public <R> void update​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> condition, com.github.collinalpert.lambda2sql.functions.SqlFunction<E,​R> column, R newValue) throws java.sql.SQLException
      Updates a specific column for records matching a condition in a table. This method is not affected by the DefaultIfNull annotation.
      Type Parameters:
      R - The data type of the column to update. It must be the same as the data type of the new value.
      Parameters:
      condition - The condition to update the column by.
      column - The column to update.
      newValue - The new value of the column.
      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​(E instance) throws java.sql.SQLException
      Deletes the corresponding row on the database.
      Parameters:
      instance - The instance to delete on the database.
      Throws:
      java.sql.SQLException - for example because of a foreign key constraint.
    • delete

      public void delete​(long id) throws java.sql.SQLException
      Deletes a row by an id.
      Parameters:
      id - The row with this id to delete.
      Throws:
      java.sql.SQLException - for example because of a foreign key constraint.
    • delete

      public void delete​(java.util.List<E> entities) throws java.sql.SQLException
      Deletes a list of entities at once.
      Parameters:
      entities - The list of entities to delete.
      Throws:
      java.sql.SQLException - for example because of a foreign key constraint.
    • delete

      public void delete​(E... entities) throws java.sql.SQLException
      Deletes multiple entities at once.
      Parameters:
      entities - A variable amount of entities.
      Throws:
      java.sql.SQLException - for example because of a foreign key constraint.
      See Also:
      delete(List)
    • delete

      public void delete​(long... ids) throws java.sql.SQLException
      Deletes multiple rows with the corresponding ids.
      Parameters:
      ids - The ids to delete the rows by.
      Throws:
      java.sql.SQLException - for example because of a foreign key constraint.
    • delete

      public void delete​(com.github.collinalpert.lambda2sql.functions.SqlPredicate<E> predicate) throws java.sql.SQLException
      Deletes rows based on a condition.
      Parameters:
      predicate - The condition to delete by.
      Throws:
      java.sql.SQLException - in case the condition cannot be applied or if a foreign key constraint fails.
    • truncateTable

      public void truncateTable() throws java.sql.SQLException
      Truncates the corresponding table on the database.
      Throws:
      java.sql.SQLException - for example because a foreign key references this table. Note that you will not be able to truncate a table, even if no foreign key references a row in this table. TRUNCATE is a DDL command and cannot check if rows are being referenced or not.