Interface BaseDao

All Known Implementing Classes:
DefaultBaseDao

public interface BaseDao
The Dao class is the interface for all database access.
  • Method Summary

    Modifier and Type Method Description
    <T extends BaseEntity>
    long
    countAll​(java.lang.Class<T> entityClass)
    Counts all rows of a type.
    <T extends BaseEntity>
    T
    create​(T obj)
    Inserts a new instance in the database.
    <T extends BaseEntity>
    void
    delete​(T obj)
    Soft deletes an object.
    static <T extends BaseEntity>
    T
    firstOrNull​(java.util.List<T> list)
    Returns null if the list is empty.
    jakarta.persistence.EntityManager getEntityManager()  
    <T extends BaseEntity>
    void
    purge​(T obj)
    Hard deletes an object.
    <T extends BaseEntity>
    T
    read​(java.lang.Class<T> entityClass, java.util.UUID id)
    Retrieves an object by ID.
    <T extends NamedEntity>
    T
    readByHandle​(java.lang.Class<T> entityClass, java.lang.String handle)
    Finds a user by handle.
    <T extends BaseEntity>
    java.util.List<T>
    readPage​(java.lang.Class<T> entityClass, int page, int pageSize)
    Returns a page of objects.
    <T extends BaseEntity>
    T
    update​(T obj)
    Updates an object.
  • Method Details

    • getEntityManager

      jakarta.persistence.EntityManager getEntityManager()
    • create

      <T extends BaseEntity> T create​(T obj)
      Inserts a new instance in the database.
      Parameters:
      obj - The object to create.
      Returns:
      The instance with ID.
    • read

      <T extends BaseEntity> T read​(java.lang.Class<T> entityClass, java.util.UUID id)
      Retrieves an object by ID.
      Parameters:
      id - The ID.
      Returns:
      The object if found; null otherwise.
    • readByHandle

      <T extends NamedEntity> T readByHandle​(java.lang.Class<T> entityClass, java.lang.String handle)
      Finds a user by handle. Returns the user on success. Returns null on failure.
      Parameters:
      handle - The user's handle.
      Returns:
      The user on success; null on failure.
    • readPage

      <T extends BaseEntity> java.util.List<T> readPage​(java.lang.Class<T> entityClass, int page, int pageSize)
      Returns a page of objects.
      Parameters:
      entityClass - The entity class.
      page - The page index (zero indexed).
      pageSize - The page size.
      Returns:
      A page of objects.
    • update

      <T extends BaseEntity> T update​(T obj)
      Updates an object.
      Parameters:
      obj - The object to update.
    • delete

      <T extends BaseEntity> void delete​(T obj)
      Soft deletes an object. The data is still in the database, but with deleted flag.
      Parameters:
      obj - The object to delete.
    • purge

      <T extends BaseEntity> void purge​(T obj)
      Hard deletes an object. This purges the data from the database.
      Parameters:
      obj - The object to delete.
    • countAll

      <T extends BaseEntity> long countAll​(java.lang.Class<T> entityClass)
      Counts all rows of a type.
      Parameters:
      entityClass - The entity class.
      Returns:
      The count of rows.
    • firstOrNull

      static <T extends BaseEntity> T firstOrNull​(java.util.List<T> list)
      Returns null if the list is empty. Returns the first element otherwise. JPA getSingleResult() throws an exception if no results, which is an annoying design. So instead you can call getResultList() and wrap it with firstOrNull(), which is the more expected result.
      Parameters:
      list -
      Returns: