Class JKObjectDataAccessImpl

java.lang.Object
com.jk.data.dataaccess.orm.JKObjectDataAccessImpl
All Implemented Interfaces:
JKObjectDataAccess

public class JKObjectDataAccessImpl extends Object implements JKObjectDataAccess
This class provides data access methods for interacting with objects.
Version:
1.0
Author:
Dr. Jalal H. Kiswani
  • Constructor Summary

    Constructors
    Constructor
    Description
    This method constructs a new JKObjectDataAccessImpl.
    This method constructs a new JKObjectDataAccessImpl with the provided data source.
    JKObjectDataAccessImpl(String dataSourcePrefix)
    This method constructs a new JKObjectDataAccessImpl with data source that is associated with the provided prefix.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    clone(T model)
    This method creates a detached copy of the provided object.
    void
    closeTransaction(boolean commit)
    This method closes the current data storage transaction.
    <T> T
    delete(Class<T> type, Object id)
    This method deletes an object from the data storage using its identifier (ID).
    <T> T
    delete(T object)
    This method deletes an object from the data storage.
    void
    detach(Object model)
    This method remove the given object from the persistence context, causing managed object to become detached.
    <T> List<T>
    executeQuery(Class<T> clas, String queryString, Object... paramters)
    This method executes a query to retrieve a list of objects of the specified class.
    <T> T
    find(Class<T> clas, Object id)
    This method finds an object from the data storage using its identifier (ID).
    <T> List<T>
    findByFieldName(Class<T> clas, String fieldName, Object value)
    This method retrieves a list of objects of the specified class based on a specific field's value.
    <T> T
    findOneByFieldName(Class<T> clas, String fieldName, Object fieldValue)
    This method retrieves a single object of the specified class from the data storage based on a field name and its value.
    <T> T
    findSingleEntity(Class<T> clas, String queryString, Object... paramters)
    This method executes a query to find a single entity of the specified class based on the given query string and parameters.
    protected JKDataSource
    This method retrieves the data source, if it's null, it returns the default data source.
    protected jakarta.persistence.EntityManager
    getEntityManager(boolean withTrx)
    This method retrieves an instance of EntityManager, optionally starting a transaction.
    <T extends JKEntity>
    T
    This method retrieves the first record of the specified entity class from the database.
    <T> List<T>
    getList(Class<T> clas)
    This method retrieves a list of objects of the provided class type from the data storage.
    <T> List<T>
    getList(Class<T> clas, Map<String,Object> paramters)
    This method retrieves a list of objects of the specified class based on the provided parameters.
    <T> List<T>
    This method retrieves a list of objects of a specified class type and caches the result.
    protected String
    getQueryOrder(Class<? extends JKEntity> clas)
    This method retrieves the ORDER BY clause for a query based on the provided JKEntity sort information.
    <T> void
    handleTimeStamps(T object)
    This method updates time stamp fields (creation and modification dates) in the given object.
    <T> T
    insert(T object)
    This method inserts the provided object into the data storage.
    <T> T
    insertOrUpdate(T object)
    This method inserts a new object into the data storage if it doesn't exist, or updates an existing object.
    void
    setMaxResults(int maxResults)
    This method sets the max results shown from any executed query.
    void
    This method starts the transaction for data storage operations.
    <T> T
    update(T object)
    This method updates an existing object in the data storage.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.jk.data.dataaccess.orm.JKObjectDataAccess

    executeSingleOutputQuery, getTenantId
  • Constructor Details

    • JKObjectDataAccessImpl

      public JKObjectDataAccessImpl()
      This method constructs a new JKObjectDataAccessImpl.
    • JKObjectDataAccessImpl

      public JKObjectDataAccessImpl(String dataSourcePrefix)
      This method constructs a new JKObjectDataAccessImpl with data source that is associated with the provided prefix.
      Parameters:
      dataSourcePrefix - Specifies the prefix associated with the data source.
    • JKObjectDataAccessImpl

      public JKObjectDataAccessImpl(JKDataSource dataSource)
      This method constructs a new JKObjectDataAccessImpl with the provided data source.
      Parameters:
      dataSource - Specifies the data source.
  • Method Details

    • insert

      public <T> T insert(T object)
      This method inserts the provided object into the data storage.
      Specified by:
      insert in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      object - Specifies the object to be inserted
      Returns:
      the inserted object.
    • update

      public <T> T update(T object)
      This method updates an existing object in the data storage.
      Specified by:
      update in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      object - Specifies the object to update.
      Returns:
      the updated object.
    • handleTimeStamps

      public <T> void handleTimeStamps(T object)
      This method updates time stamp fields (creation and modification dates) in the given object.
      Type Parameters:
      T - Specifies the type of object to update.
      Parameters:
      object - Specifies the object for which to update time stamps.
    • find

      public <T> T find(Class<T> clas, Object id)
      This method finds an object from the data storage using its identifier (ID).
      Specified by:
      find in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      clas - Specifies the class type of the object.
      id - Specifies the identifier (ID) of the object to find.
      Returns:
      the found object, or null if not found.
    • delete

      public <T> T delete(T object)
      This method deletes an object from the data storage.
      Specified by:
      delete in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      object - Specifies the object to delete.
      Returns:
      the deleted object.
    • delete

      public <T> T delete(Class<T> type, Object id)
      This method deletes an object from the data storage using its identifier (ID).
      Specified by:
      delete in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      type - Specifies the class type of the object.
      id - Specifies the object identifier (ID)
      Returns:
      the deleted object.
    • getList

      public <T> List<T> getList(Class<T> clas)
      This method retrieves a list of objects of the provided class type from the data storage.
      Specified by:
      getList in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      clas - Specifies the class type of the objects.
      Returns:
      a list of objects from data storage that are of the provided class type.
    • getListAndCache

      public <T> List<T> getListAndCache(Class<T> clas)
      This method retrieves a list of objects of a specified class type and caches the result.
      Specified by:
      getListAndCache in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object.
      Parameters:
      clas - Specifies the class type of the objects to retrieve.
      Returns:
      a list of objects of the specified class type.
    • getList

      public <T> List<T> getList(Class<T> clas, Map<String,Object> paramters)
      This method retrieves a list of objects of the specified class based on the provided parameters.
      Specified by:
      getList in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of objects to retrieve.
      Parameters:
      clas - Specifies the class of objects to retrieve.
      paramters - Specifies a map of parameters or criteria for filtering the objects.
      Returns:
      a list of objects matching the specified class and criteria.
    • executeQuery

      public <T> List<T> executeQuery(Class<T> clas, String queryString, Object... paramters)
      This method executes a query to retrieve a list of objects of the specified class.
      Specified by:
      executeQuery in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of objects to retrieve.
      Parameters:
      clas - Specifies the class of objects to retrieve.
      queryString - Specifies the query string to execute.
      paramters - Specifies the optional parameters to bind to the query.
      Returns:
      a list of objects matching the specified class and query criteria.
    • getEntityManager

      protected jakarta.persistence.EntityManager getEntityManager(boolean withTrx)
      This method retrieves an instance of EntityManager, optionally starting a transaction.
      Parameters:
      withTrx - Indicates whether a transaction should be started (true) or not (false).
      Returns:
      an EntityManager instance.
    • getDataSource

      protected JKDataSource getDataSource()
      This method retrieves the data source, if it's null, it returns the default data source.
      Returns:
      the data source.
    • getQueryOrder

      protected String getQueryOrder(Class<? extends JKEntity> clas) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
      This method retrieves the ORDER BY clause for a query based on the provided JKEntity sort information.
      Parameters:
      clas - Specifies the JKEntity for which to retrieve the ORDER BY clause.
      Returns:
      the query order as a string.
      Throws:
      NoSuchMethodException - if a required method is not found.
      IllegalAccessException - if access to a method is denied.
      InvocationTargetException - if a method invocation fails.
    • findSingleEntity

      public <T> T findSingleEntity(Class<T> clas, String queryString, Object... paramters)
      This method executes a query to find a single entity of the specified class based on the given query string and parameters.
      Type Parameters:
      T - Specifies the type of the entity.
      Parameters:
      clas - Specifies the class of the entity to be retrieved.
      queryString - Specifies the query string.
      paramters - Specifies the query parameters.
      Returns:
      the found entity or null if none was found.
    • getFirstRecord

      public <T extends JKEntity> T getFirstRecord(Class<T> clas)
      This method retrieves the first record of the specified entity class from the database.
      Type Parameters:
      T - Specifies the type of the entity.
      Parameters:
      clas - Specifies the class of the entity to be retrieved.
      Returns:
      the first record of the specified entity class or null if none exists.
    • findByFieldName

      public <T> List<T> findByFieldName(Class<T> clas, String fieldName, Object value)
      This method retrieves a list of objects of the specified class based on a specific field's value.
      Specified by:
      findByFieldName in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of objects to retrieve.
      Parameters:
      clas - Specifies the class of objects to retrieve.
      fieldName - Specifies the name of the field to search by.
      value - Specifies the value to search for within the specified field.
      Returns:
      a list of objects matching the specified class and field value criteria.
    • insertOrUpdate

      public <T> T insertOrUpdate(T object)
      This method inserts a new object into the data storage if it doesn't exist, or updates an existing object.
      Specified by:
      insertOrUpdate in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object to insert or update.
      Parameters:
      object - Specifies the object to insert or update in the data storage.
      Returns:
      the inserted or updated object.
    • findOneByFieldName

      public <T> T findOneByFieldName(Class<T> clas, String fieldName, Object fieldValue)
      This method retrieves a single object of the specified class from the data storage based on a field name and its value.
      Specified by:
      findOneByFieldName in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of object to retrieve.
      Parameters:
      clas - Specifies the class of the object to retrieve.
      fieldName - Specifies the name of the field to use as a filter.
      fieldValue - Specifies the value to match in the specified field.
      Returns:
      the retrieved object, or null if no matching object is found.
    • setMaxResults

      public void setMaxResults(int maxResults)
      This method sets the max results shown from any executed query.
      Specified by:
      setMaxResults in interface JKObjectDataAccess
      Parameters:
      maxResults - Specifies the new max results shown from any executed query.
    • detach

      public void detach(Object model)
      This method remove the given object from the persistence context, causing managed object to become detached.
      Specified by:
      detach in interface JKObjectDataAccess
      Parameters:
      model - Specifies the object to detach from the persistence context.
    • clone

      public <T> T clone(T model)
      This method creates a detached copy of the provided object.
      Specified by:
      clone in interface JKObjectDataAccess
      Type Parameters:
      T - Specifies the type of the object to clone.
      Parameters:
      model - Specifies the object to create a detached copy of.
      Returns:
      a detached copy of the input object.
    • startTransaction

      public void startTransaction()
      This method starts the transaction for data storage operations.
      Specified by:
      startTransaction in interface JKObjectDataAccess
    • closeTransaction

      public void closeTransaction(boolean commit)
      This method closes the current data storage transaction.
      Specified by:
      closeTransaction in interface JKObjectDataAccess
      Parameters:
      commit - Indicates whether to commit (true) the transaction or to roll it back (false).