Class DatabaseProxy

java.lang.Object
io.john.amiscaray.quak.data.DatabaseProxy

public class DatabaseProxy extends Object
A class used to perform database operations. Used to create hibernate sessions and perform CRUD operations.
  • Constructor Details

    • DatabaseProxy

      public DatabaseProxy(String classScanPackage)
      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

      public <T> boolean existsById(Object entityID, Class<T> entityType)
      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

      public void persist(Object entity)
      Saves an entity to the database.
      Parameters:
      entity - The entity.
    • put

      public <T> boolean put(Object entity, Object entityID, Class<T> entityType)
      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

      public <T> boolean patch(Object entity, Object entityID, Class<T> entityType)
      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

      public <T> T fetchById(Object entityId, Class<T> entityType)
      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

      public void delete(Object entityId, Class<?> entityType)
      Deletes an entity from the database by its ID.
      Parameters:
      entityId - The ID of the entity.
      entityType - The type of the entity.
    • queryAll

      public <T> List<T> queryAll(Class<T> entityType)
      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

      public <T> List<T> queryAll(Class<T> entityType, DatabaseQuery databaseQuery)
      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

      public <T> List<T> queryAllWhere(Class<T> entityType, QueryCriteria criteria)
      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

      public <T> void deleteAll(Class<T> entityType, DatabaseQuery deletionCriteria)
      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

      public <T> void deleteAllWhere(Class<T> entityType, QueryCriteria criteria)
      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

      public final <T, F> void updateAll(Class<T> entityType, FieldUpdate<F> fieldUpdate)
      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

      public <T, V> void updateAll(Class<T> entityType, String fieldToUpdate, V newValue)
      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

      public void createMutationQueryThen(String hql, Consumer<org.hibernate.query.MutationQuery> action)
      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 an NativeQuery.
      Type Parameters:
      R - The type of the result.
      Parameters:
      query - The query.
      entityType - The type of the entity.
      Returns:
      The selection query.