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.ObjectClass that provides base functionality for all service classes. Every service class must extend this class.- Author:
- Collin Alpert
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseService()Constructor for the base class of all services.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcreate(T instance)Creates this 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 Query<T>getMultiple(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Retrieves list of entities which match the predicate.protected java.util.Optional<T>getSingle(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)Retrieves a single entity which matches the predicate.voidupdate(T instance)Updates this entity's row on the database.
-
-
-
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.
-
getSingle
protected java.util.Optional<T> getSingle(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Retrieves a single entity which matches the predicate. It isprotectedas 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- TheSqlPredicateto add constraints to a DQL query.- Returns:
- An entity matching the result of the query.
-
getMultiple
protected Query<T> getMultiple(com.github.collinalpert.lambda2sql.functions.SqlPredicate<T> predicate)
Retrieves list of entities which match the predicate. It isprotectedas 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- TheSqlPredicateto 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.
-
-