Class OBDal

  • All Implemented Interfaces:
    OBNotSingleton, OBProvidable

    public class OBDal
    extends Object
    implements OBNotSingleton
    The OBDal class offers the main external access to the Data Access Layer. The variety of data access methods are provided such as save, get, query, remove, etc.
    Author:
    mtaal
    See Also:
    OBCriteria, OBQuery
    • Constructor Detail

      • OBDal

        public OBDal()
    • Method Detail

      • getInstance

        public static OBDal getInstance()
        Returns:
        the singleton instance of the OBDal service
      • getReadOnlyInstance

        public static OBDal getReadOnlyInstance()
        This method tries to return a read-only instance if the read-only pool is enabled in the configuration, the Preference "OBUIAPP_DefaultDBPoolForReports" is set to "RO" or there is a DataPoolSelection entry for the current process set to "RO". Otherwise, the default pool is returned.
        Returns:
        the singleton instance of the OBDal read-only service if possible. In any other case, the default instance will be returned.
      • getInstance

        public static OBDal getInstance​(String pool)
        Parameters:
        pool - the name of the pool used by the OBDal service that will be returned
        Returns:
        the singleton instance related to the name passed as parameter
      • isActiveFilterEnabled

        public boolean isActiveFilterEnabled()
        Returns the status of the active filter.
        Returns:
        true if the active filter is enabled, false if it is disabled
      • getConnection

        public Connection getConnection()
        Returns the connection used by the hibernate session. Note: flushes the hibernate session before returning the connection.
        Returns:
        the current database connection
        See Also:
        flush()
      • getConnection

        public Connection getConnection​(boolean doFlush)
        Returns the connection used by the hibernate session.
        Parameters:
        doFlush - if true then the current actions are first flushed.
        Returns:
        the current database connection
        See Also:
        flush()
      • getSession

        public org.hibernate.Session getSession()
        Returns:
        the current hibernate session
      • isSessionDirty

        public boolean isSessionDirty()
        Checks if the session associated with this DAL instance is dirty. Note isDirty method should not be directly invoked on the Session instance.
        See Also:
        SessionHandler.isSessionDirty(String)
      • commitAndClose

        public void commitAndClose()
        Commits the transaction and closes session.
      • rollbackAndClose

        public void rollbackAndClose()
        Rolls back the transaction and closes the session.
      • flush

        public void flush()
        Flushes the current state to the database.
      • save

        public void save​(Object obj)
        Sets the client and organization of the object (if not set) and persists the object in the database.
        Parameters:
        obj - the object to persist
      • remove

        public void remove​(Object obj)
        Removes the object from the database.
        Parameters:
        obj - the object to be removed
      • refresh

        public void refresh​(Object obj)
        Refresh the given object from the database. Also initialized lists inside the object will be refreshed.
        Parameters:
        obj - the object to refresh
        See Also:
        Session.refresh(Object)
      • get

        public <T> T get​(Class<T> clazz,
                         Object id)
        Retrieves an object from the database using the class and id.
        Parameters:
        clazz - the type of object to search for
        id - the id of the object
        Returns:
        the object, or null if none found
      • exists

        public boolean exists​(String entityName,
                              Object id)
        Returns true if an object (identified by the entityName and id) exists, false otherwise.
        Parameters:
        entityName - the name of the entity
        id - the id used to find the instance
        Returns:
        true if exists, false otherwise
      • get

        public BaseOBObject get​(String entityName,
                                Object id)
        Retrieves an object from the database using the entity name and id.
        Parameters:
        entityName - the type of object to search for
        id - the id of the object
        Returns:
        the object, or null if none found
      • getProxy

        public BaseOBObject getProxy​(String entityName,
                                     Object id)
        Will return a non-loaded hibernate proxy if the object was not already loaded by hibernate. NOTE/BEWARE: this method will not check if the object actually exists in the database. This will detected when persisting a referencing object or when this proxy gets initialized! This method differs from other get methods in this class, these methods will always eagerly load the object and thereby also immediately check the existence of these referenced objects.
        Parameters:
        entityName - the type of object to search for
        id - the id of the object
        Returns:
        the object, or null if none found
      • createQuery

        public <T extends BaseOBObjectOBQuery<T> createQuery​(Class<T> fromClz,
                                                               String whereOrderByClause)
        Creates an OBQuery object using a class and a specific where and order by clause.
        Parameters:
        fromClz - the class to create the query for
        whereOrderByClause - the HQL where and orderby clause
        Returns:
        the query object
      • createQuery

        @Deprecated
        public <T extends BaseOBObjectOBQuery<T> createQuery​(Class<T> fromClz,
                                                               String whereOrderByClause,
                                                               List<Object> parameters)
        Deprecated.
        Creates an OBQuery object using a class and a specific where and order by clause and a set of parameters which are used in the query.
        Parameters:
        fromClz - the class to create the query for
        whereOrderByClause - the HQL where and orderby clause
        parameters - the parameters to use in the query
        Returns:
        the query object
      • createQuery

        public <T extends BaseOBObjectOBQuery<T> createQuery​(Class<T> fromClz,
                                                               String whereOrderByClause,
                                                               Map<String,​Object> parameters)
        Creates an OBQuery object using a class and a specific where and order by clause and a map of named parameters which are used in the query.
        Parameters:
        fromClz - the class to create the query for
        whereOrderByClause - the HQL where and orderby clause
        parameters - the named parameters to use in the query
        Returns:
        the query object
      • createQuery

        public OBQuery<BaseOBObject> createQuery​(String entityName,
                                                 String whereOrderByClause)
        Creates an OBQuery object using an entity name and a specific where and order by clause.
        Parameters:
        entityName - the type to create the query for
        whereOrderByClause - the HQL where and orderby clause
        Returns:
        the new query object
      • createQuery

        @Deprecated
        public OBQuery<BaseOBObject> createQuery​(String entityName,
                                                 String whereOrderByClause,
                                                 List<Object> parameters)
        Deprecated.
        Creates an OBQuery object using an entity name and a specific where and order by clause and a set of parameters which are used in the query.
        Parameters:
        entityName - the type to create the query for
        whereOrderByClause - the HQL where and orderby clause
        parameters - the parameters to use in the query
        Returns:
        a new instance of OBQuery.
      • createQuery

        public OBQuery<BaseOBObject> createQuery​(String entityName,
                                                 String whereOrderByClause,
                                                 Map<String,​Object> parameters)
        Creates an OBQuery object using an entity name and a specific where and order by clause and a map of named parameters which are used in the query.
        Parameters:
        entityName - the type to create the query for
        whereOrderByClause - the HQL where and orderby clause
        parameters - the named parameters to use in the query
        Returns:
        a new instance of OBQuery.
      • createCriteria

        public <T extends BaseOBObjectOBCriteria<T> createCriteria​(Class<T> clz)
        Creates an OBCriteria object for the specified class.
        Parameters:
        clz - the class used to create the OBCriteria
        Returns:
        a new OBCriteria object
      • createCriteria

        public <T extends BaseOBObjectOBCriteria<T> createCriteria​(Class<T> clz,
                                                                     String alias)
        Creates an OBCriteria object for the specified class.
        Parameters:
        clz - the class used to create the OBCriteria
        alias - an alias that can be used to refer to the specified object
        Returns:
        a new OBCriteria object
      • createCriteria

        public <T extends BaseOBObjectOBCriteria<T> createCriteria​(String entityName)
        Creates an OBCriteria object for the specified entity.
        Parameters:
        entityName - the type used to create the OBCriteria
        Returns:
        a new OBCriteria object
      • createCriteria

        public <T extends BaseOBObjectOBCriteria<T> createCriteria​(String entityName,
                                                                     String alias)
        Creates an OBCriteria object for the specified entity.
        Parameters:
        entityName - the type used to create the OBCriteria
        alias - an alias that can be used to refer to the specified object
        Returns:
        a new OBCriteria object
      • findUniqueConstrainedObjects

        public List<BaseOBObject> findUniqueConstrainedObjects​(BaseOBObject obObject)
        Retrieves a list of baseOBObjects using the unique-constraints defined for the entity. The passed BaseOBObject and the unique-constraints are used to construct a query searching for matching objects in the database.

        Note that multiple unique constraints are used, so therefore the result can be more than one object.

        Parameters:
        obObject - this property values of this obObject is used to find other objects in the database with the same property values for the unique constraint properties
        Returns:
        a list of objects which match the passed obObject on the unique constraint properties
        See Also:
        Entity.getUniqueConstraints()
      • setClientOrganization

        protected void setClientOrganization​(Object o)
      • getReadableOrganizationsInClause

        @Deprecated
        public String getReadableOrganizationsInClause()
        Deprecated.
        Use bind statement parameter instead of this method
        Returns an in-clause HQL clause denoting the organizations which are allowed to be read by the current user. The in-clause can be directly used in a HQL. The return string will be for example: in ('1000000', '1000001')
        Returns:
        an in-clause which can be directly used inside of a HQL clause
        See Also:
        OBContext.getReadableOrganizations()
      • getReadableClientsInClause

        @Deprecated
        public String getReadableClientsInClause()
        Deprecated.
        Use bind statement parameter instead of this method
        Returns an in-clause HQL clause denoting the clients which are allowed to be read by the current user. The in-clause can be directly used in a HQL. The return string will be for example: in ('1000000', '1000001')
        Returns:
        an in-clause which can be directly used inside of a HQL clause
        See Also:
        OBContext.getReadableClients()
      • getObjectLockForNoKeyUpdate

        public <T extends BaseOBObject> T getObjectLockForNoKeyUpdate​(T object)
        Creates a WRITE lock in database for the DAL persistence instance object parameter and returns a new instance representing the same database object.

        Note the original instance that is passed as parameter is evicted from Hibernate's 1st level. Therefore, any state not persisted before invoking this method will be ignored, after invoking this method the parameter instance shouldn't be used anymore using instead the returned one.

        Whereas this is similar to JPA's LockModeType.PESSIMISTIC_WRITE, it decreases lock level in PostgreSQL implemented by Hibernate from FOR UPDATE to FOR NO KEY UPDATE allowing insertions of children records while a lock on its parent is acquired by a different transaction. This is a workaround until Hibernate issue HHH-13135 is fixed. Unlike locks acquired by Hibernate, the ones created by this method are only present in Database and cannot be detected by Hibernate (eg. Session.getCurrentLockMode(Object).

        Parameters:
        object - DAL instance to acquire a database lock for.
        Returns:
        A new DAL instance that represents the same database object than the parameter.