Package org.minijax.dao
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>
longcountAll(java.lang.Class<T> entityClass)Counts all rows of a type.<T extends BaseEntity>
Tcreate(T obj)Inserts a new instance in the database.<T extends BaseEntity>
voiddelete(T obj)Soft deletes an object.static <T extends BaseEntity>
TfirstOrNull(java.util.List<T> list)Returns null if the list is empty.jakarta.persistence.EntityManagergetEntityManager()<T extends BaseEntity>
voidpurge(T obj)Hard deletes an object.<T extends BaseEntity>
Tread(java.lang.Class<T> entityClass, java.util.UUID id)Retrieves an object by ID.<T extends NamedEntity>
TreadByHandle(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>
Tupdate(T obj)Updates an object.
-
Method Details
-
getEntityManager
jakarta.persistence.EntityManager getEntityManager() -
create
Inserts a new instance in the database.- Parameters:
obj- The object to create.- Returns:
- The instance with ID.
-
read
Retrieves an object by ID.- Parameters:
id- The ID.- Returns:
- The object if found; null otherwise.
-
readByHandle
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
Updates an object.- Parameters:
obj- The object to update.
-
delete
Soft deletes an object. The data is still in the database, but with deleted flag.- Parameters:
obj- The object to delete.
-
purge
Hard deletes an object. This purges the data from the database.- Parameters:
obj- The object to delete.
-
countAll
Counts all rows of a type.- Parameters:
entityClass- The entity class.- Returns:
- The count of rows.
-
firstOrNull
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:
-