java.lang.Object
io.john.amiscaray.quak.data.DatabaseProxy
A class used to perform database operations. Used to create hibernate sessions and perform CRUD operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidBegins a hibernate sessionvoidcreateMutationQueryThen(String hql, Consumer<org.hibernate.query.MutationQuery> action) Creates a mutation query using an HQL string and runs it in a transaction.<R> org.hibernate.query.SelectionQuery<R> createSelectionQuery(NativeQuery query, Class<R> entityType) Creates a selection query from an HQL string and its parameters represented as anNativeQuery.<R> voidcreateSelectionQueryThen(String hql, Class<R> entityType, Consumer<org.hibernate.query.SelectionQuery<R>> action) Creates a selection query from an HQL string.voidDeletes an entity from the database by its ID.<T> voiddeleteAll(Class<T> entityType, DatabaseQuery deletionCriteria) Deletes all entities from the database matching a given query.<T> voiddeleteAllWhere(Class<T> entityType, QueryCriteria criteria) Deletes all entities from the database matching a given query.voidEnds the hibernate session<T> booleanexistsById(Object entityID, Class<T> entityType) Checks if an entity exists based on its ID.<T> TFetches an entity from the database by its ID.<T> booleanPerforms a PATCH operation on the database.voidSaves an entity to the database.<T> booleanPerforms a PUT operation on the database.<T> List<T> Retrieves all entities of a given type from the database.<T> List<T> queryAll(Class<T> entityType, DatabaseQuery databaseQuery) Retrieves all entities of a given type from the database which match a given query.<T> List<T> queryAllWhere(Class<T> entityType, QueryCriteria criteria) Retrieves all entities of a given type from the database which match a given query.final <T,F> void updateAll(Class<T> entityType, DatabaseQuery updateCriteria, FieldUpdate<F> fieldUpdate) Updates all entities from the database matching some update criteria.final <T,F> void updateAll(Class<T> entityType, FieldUpdate<F> fieldUpdate) Updates all entities of the same type.<T,V> void updateAll(Class<T> entityType, String fieldToUpdate, DatabaseQuery updateCriteria, V newValue) Updates entities based on a given criteria to a new value.<T,V> void Updates all entities of the same type with a new value.final <T,F> void updateAllWhereAndApply(Class<T> entityType, QueryCriteria queryCriteria, FieldUpdate<F> fieldUpdate) Updates all entities from the database matching some update criteria.<T,V> void updateAllWhereAndSetTo(Class<T> entityType, String fieldToUpdate, QueryCriteria updateCriteria, V newValue) Updates entities based on a given criteria to a new value.
-
Constructor Details
-
DatabaseProxy
Initialize the DatabaseProxy. Looks in the project packages for hibernate entities.- Parameters:
classScanPackage- The root package of the project to scan for entity classes from.
-
-
Method Details
-
beginSession
public void beginSession()Begins a hibernate session -
endSession
public void endSession()Ends the hibernate session -
existsById
Checks if an entity exists based on its ID.- Type Parameters:
T- matches the type of the entity.- Parameters:
entityID- The ID to test for.entityType- The class representing the database entity.- Returns:
- Whether there is an entity with the given ID.
-
persist
Saves an entity to the database.- Parameters:
entity- The entity.
-
put
Performs a PUT operation on the database.- Type Parameters:
T- The type of the entity.- Parameters:
entity- The entity to update or save to the database.entityID- The ID of the entity.entityType- The class of the entity.- Returns:
- Whether the operation was an update or a creation.
-
patch
Performs a PATCH operation on the database.- Type Parameters:
T- The type of the entity.- Parameters:
entity- The updated entity.entityID- The ID of the entity to update.entityType- The type of the entity.- Returns:
- Whether the operation was successful.
-
fetchById
Fetches an entity from the database by its ID.- Type Parameters:
T- The type of the entity.- Parameters:
entityId- The ID of the entity.entityType- The type of the entity.- Returns:
- The entity.
-
delete
Deletes an entity from the database by its ID.- Parameters:
entityId- The ID of the entity.entityType- The type of the entity.
-
queryAll
Retrieves all entities of a given type from the database.- Type Parameters:
T- The type of the entity.- Parameters:
entityType- The type of the entity.- Returns:
- A list of all the entities.
-
queryAll
Retrieves all entities of a given type from the database which match a given query.- Type Parameters:
T- The type of the entity.- Parameters:
entityType- The type of the entity.databaseQuery- The query to filter entities using.- Returns:
- A list of all the entities.
-
queryAllWhere
Retrieves all entities of a given type from the database which match a given query.- Type Parameters:
T- The type of the entity.- Parameters:
entityType- The type of the entity.criteria- The query to filter entities using.- Returns:
- A list of all the entities.
-
deleteAll
Deletes all entities from the database matching a given query.- Type Parameters:
T- The type of the entity.- Parameters:
entityType- The type of the entity.deletionCriteria- A database query for the deletion criteria.
-
deleteAllWhere
Deletes all entities from the database matching a given query.- Type Parameters:
T- The type of the entity.- Parameters:
entityType- The type of the entity.criteria- Query criteria for the deletion.
-
updateAll
public final <T,F> void updateAll(Class<T> entityType, DatabaseQuery updateCriteria, FieldUpdate<F> fieldUpdate) Updates all entities from the database matching some update criteria.- Type Parameters:
T- The type of the entity.F- The type of the field being updated.- Parameters:
entityType- The type of the entity.updateCriteria- A database query for the update criteria.fieldUpdate- The updates made to the field.
-
updateAllWhereAndApply
public final <T,F> void updateAllWhereAndApply(Class<T> entityType, QueryCriteria queryCriteria, FieldUpdate<F> fieldUpdate) Updates all entities from the database matching some update criteria.- Type Parameters:
T- The type of the entity.F- The type of the field being updated.- Parameters:
entityType- The type of the entity.queryCriteria- The query criteria to select rows to update.fieldUpdate- The updates made to the field.
-
updateAll
Updates all entities of the same type.- Type Parameters:
T- The type of the entity.F- The type of the field.- Parameters:
entityType- The type of the entity.fieldUpdate- The updates made to the field.
-
updateAll
public <T,V> void updateAll(Class<T> entityType, String fieldToUpdate, DatabaseQuery updateCriteria, V newValue) Updates entities based on a given criteria to a new value.- Type Parameters:
T- The type of the entity.V- The type of the field.- Parameters:
entityType- The type of the entity.fieldToUpdate- The name of the field to update.updateCriteria- A query for the update criteria.newValue- The new value to set to the field.
-
updateAllWhereAndSetTo
public <T,V> void updateAllWhereAndSetTo(Class<T> entityType, String fieldToUpdate, QueryCriteria updateCriteria, V newValue) Updates entities based on a given criteria to a new value.- Type Parameters:
T- The type of the entity.V- The type of the field.- Parameters:
entityType- The type of the entity.fieldToUpdate- The name of the field to update.updateCriteria- The query criteria to select fields for the update.newValue- The new value to set to the field.
-
updateAll
Updates all entities of the same type with a new value.- Type Parameters:
T- The type of the entity.V- The type of the field.- Parameters:
entityType- The type of the entity.fieldToUpdate- The name of the field to update.newValue- The new value of the field.
-
createMutationQueryThen
Creates a mutation query using an HQL string and runs it in a transaction. Example:
dbProxy.createMutationQueryThen( "UPDATE Employee e SET e.department = :newDepartment", query -> query.setParameter("newDepartment", "Tech").executeUpdate() );- Parameters:
hql- The HQL query.action- A consumer of the created mutation query. Runs within the transaction.
-
createSelectionQueryThen
public <R> void createSelectionQueryThen(String hql, Class<R> entityType, Consumer<org.hibernate.query.SelectionQuery<R>> action) Creates a selection query from an HQL string. Example:
dbProxy.createSelectionQueryThen("FROM Employee WHERE department = 'Tech'", Employee.class, query -> { assertEquals( List.of( new Employee(1L, "Billy", "Tech", 40000L), new Employee(2L, "Elli", "Tech", 40000L), new Employee(3L, "John", "Tech", 40000L) ), query.getResultList() ); });- Type Parameters:
R- The type of the result.- Parameters:
hql- The HQL string.entityType- The type of the entity.action- A consumer of the selection criteria.
-
createSelectionQuery
public <R> org.hibernate.query.SelectionQuery<R> createSelectionQuery(NativeQuery query, Class<R> entityType) Creates a selection query from an HQL string and its parameters represented as anNativeQuery.- Type Parameters:
R- The type of the result.- Parameters:
query- The query.entityType- The type of the entity.- Returns:
- The selection query.
-