Class BaseService<T extends BaseEntity>
- java.lang.Object
-
- com.github.collinalpert.java2db.services.BaseService<T>
-
- 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> clazz)Constructor for the base class of all services.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancreate(T instance)Creates a Java entity on the database.voiddelete(long id)Deletes a row by an id.voiddelete(T instance)Deletes the corresponding row on the database.java.util.List<T>getAll()java.util.Optional<T>getById(long id)protected java.sql.ResultSetgetByPredicate(SqlPredicate<T> predicate, DBConnection connection)java.util.List<T>getMultiple(SqlPredicate<T> predicate)java.util.Optional<T>getSingle(SqlPredicate<T> predicate)booleanupdate(T instance)Applies updates to an entity on the database.
-
-
-
Constructor Detail
-
BaseService
public BaseService(java.lang.Class<T> clazz)
Constructor for the base class of all services. It is possible to create instances of it.- Parameters:
clazz- The entity class corresponding to this service class.
-
-
Method Detail
-
create
public boolean create(T instance)
Creates a Java entity on the database.- Parameters:
instance- The instance to create on the database.- Returns:
Trueif the INSERT was successful,falseif not.
-
getByPredicate
protected java.sql.ResultSet getByPredicate(SqlPredicate<T> predicate, DBConnection connection)
- Parameters:
predicate- TheSqlPredicatefor constraints on the SELECT query.connection- A connection to execute the query on.- Returns:
- A
ResultSetby a predicate.
-
getSingle
public java.util.Optional<T> getSingle(SqlPredicate<T> predicate)
- Parameters:
predicate- TheSqlPredicateto add constraints to a SELECT query.- Returns:
- An entity matching the result of the query.
-
getMultiple
public java.util.List<T> getMultiple(SqlPredicate<T> predicate)
- Parameters:
predicate- TheSqlPredicateto add constraints to a SELECT query.- 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 boolean update(T instance)
Applies updates to an entity on the database.- Parameters:
instance- The instance to update on the interface.- Returns:
Trueif the update is successful,falseif not.
-
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.
-
-