Interface EntityConnection

    • Method Detail

      • entities

        Entities entities()
        Returns:
        the underlying domain entities
      • user

        User user()
        Returns:
        the user being used by this connection
      • isConnected

        boolean isConnected()
        Returns:
        true if the connection has been established and is valid
      • close

        void close()
        Performs a rollback and disconnects this connection
        Specified by:
        close in interface AutoCloseable
      • isTransactionOpen

        boolean isTransactionOpen()
        Returns:
        true if a transaction is open, false otherwise
      • beginTransaction

        void beginTransaction()
        Begins a transaction on this connection
        Throws:
        IllegalStateException - if a transaction is already open
      • rollbackTransaction

        void rollbackTransaction()
        Performs a rollback and ends the current transaction
        Throws:
        IllegalStateException - in case a transaction is not open
      • commitTransaction

        void commitTransaction()
        Performs a commit and ends the current transaction
        Throws:
        IllegalStateException - in case a transaction is not open
      • setQueryCacheEnabled

        void setQueryCacheEnabled​(boolean queryCacheEnabled)
        Controls the enabled state of the query result cache. Queries are cached on a SelectCondition basis, but never when selecting for update. The cache is cleared when disabled.
        Parameters:
        queryCacheEnabled - the result cache state
      • executeFunction

        <C extends EntityConnection,​T,​R> R executeFunction​(FunctionType<C,​T,​R> functionType)
                                                                throws DatabaseException
        Executes the function with the given type with no arguments
        Type Parameters:
        C - the connection type
        T - the argument type
        R - the return value type
        Parameters:
        functionType - the function type
        Returns:
        the function return value
        Throws:
        DatabaseException - in case anything goes wrong during the execution
      • executeFunction

        <C extends EntityConnection,​T,​R> R executeFunction​(FunctionType<C,​T,​R> functionType,
                                                                       T argument)
                                                                throws DatabaseException
        Executes the function with the given type
        Type Parameters:
        C - the connection type
        T - the argument type
        R - the return value type
        Parameters:
        functionType - the function type
        argument - the function argument
        Returns:
        the function return value
        Throws:
        DatabaseException - in case anything goes wrong during the execution
      • executeProcedure

        <C extends EntityConnection,​T> void executeProcedure​(ProcedureType<C,​T> procedureType)
                                                            throws DatabaseException
        Executes the procedure with the given type with no arguments
        Type Parameters:
        C - the connection type
        T - the procedure argument type
        Parameters:
        procedureType - the procedure type
        Throws:
        DatabaseException - in case anything goes wrong during the execution
      • executeProcedure

        <C extends EntityConnection,​T> void executeProcedure​(ProcedureType<C,​T> procedureType,
                                                                   T argument)
                                                            throws DatabaseException
        Executes the procedure with the given type
        Type Parameters:
        C - the connection type
        T - the argument type
        Parameters:
        procedureType - the procedure type
        argument - the procedure argument
        Throws:
        DatabaseException - in case anything goes wrong during the execution
      • insert

        Key insert​(Entity entity)
            throws DatabaseException
        Inserts the given entity, returning the primary key. Performs a commit unless a transaction is open.
        Parameters:
        entity - the entity to insert
        Returns:
        the primary key of the inserted entity
        Throws:
        DatabaseException - in case of a database exception
      • insert

        List<Key> insert​(List<? extends Entity> entities)
                  throws DatabaseException
        Inserts the given entities, returning a list containing the primary keys in the same order as they were received. Performs a commit unless a transaction is open.
        Parameters:
        entities - the entities to insert
        Returns:
        the primary key values of the inserted entities
        Throws:
        DatabaseException - in case of a database exception
      • update

        Entity update​(Entity entity)
               throws DatabaseException
        Updates the given entity based on its attribute values. Returns the updated entity. Throws an exception if the given entity is unmodified. Performs a commit unless a transaction is open.
        Parameters:
        entity - the entity to update
        Returns:
        the updated entity
        Throws:
        DatabaseException - in case of a database exception
        UpdateException - in case there is a mismatch between expected and actual number of updated rows
        RecordModifiedException - in case the entity has been modified or deleted by another user
      • update

        List<Entity> update​(List<? extends Entity> entities)
                     throws DatabaseException
        Updates the given entities based on their attribute values. Returns the updated entities, in no particular order. Throws an exception if any of the given entities is unmodified. Performs a commit unless a transaction is open.
        Parameters:
        entities - the entities to update
        Returns:
        the updated entities, in no particular order
        Throws:
        DatabaseException - in case of a database exception
        UpdateException - in case there is a mismatch between expected and actual number of updated rows
        RecordModifiedException - in case an entity has been modified or deleted by another user
      • delete

        void delete​(Key entityKey)
             throws DatabaseException
        Deletes the entity with the given primary key. Performs a commit unless a transaction is open.
        Parameters:
        entityKey - the primary key of the entity to delete
        Throws:
        DatabaseException - in case of a database exception
        DeleteException - in case no row or multiple rows were deleted
      • delete

        void delete​(Collection<Key> entityKeys)
             throws DatabaseException
        Deletes the entities with the given primary keys. Performs a commit unless a transaction is open.
        Parameters:
        entityKeys - the primary keys of the entities to delete
        Throws:
        DatabaseException - in case of a database exception
        DeleteException - in case the number of deleted rows does not match the number of keys
      • delete

        int delete​(Condition condition)
            throws DatabaseException
        Deletes the entities specified by the given condition. Performs a commit unless a transaction is open.
        Parameters:
        condition - the condition specifying the entities to delete
        Returns:
        the number of deleted rows
        Throws:
        DatabaseException - in case of a database exception
      • select

        <T> List<T> select​(Attribute<T> attribute,
                           Condition condition)
                    throws DatabaseException
        Selects ordered and distinct non-null values of the given attribute, note that the attribute must be associated with a ColumnProperty.
        Type Parameters:
        T - the value type
        Parameters:
        attribute - attribute
        condition - the condition, null if all values should be selected
        Returns:
        the values of the given attribute
        Throws:
        DatabaseException - in case of a database exception
        IllegalArgumentException - in case the given property is not a column based attribute
        UnsupportedOperationException - in case the entity is based on a select query
      • selectSingle

        <T> Entity selectSingle​(Attribute<T> attribute,
                                T value)
                         throws DatabaseException
        Selects a single entity
        Type Parameters:
        T - the value type
        Parameters:
        attribute - attribute to use as a condition
        value - the value to use in the condition
        Returns:
        an entity of the type entityType, having the value of attribute as value
        Throws:
        DatabaseException - in case of a database exception
        RecordNotFoundException - in case the entity was not found
        MultipleRecordsFoundException - in case multiple entities were found
      • select

        List<Entity> select​(Condition condition)
                     throws DatabaseException
        Selects entities based on the given condition
        Parameters:
        condition - the condition specifying which entities to select
        Returns:
        entities based to the given condition
        Throws:
        DatabaseException - in case of a database exception
      • select

        <T> List<Entity> select​(Attribute<T> attribute,
                                T value)
                         throws DatabaseException
        Selects entities based on a single attribute condition
        Type Parameters:
        T - the value type
        Parameters:
        attribute - the condition attribute
        value - the value to use as condition
        Returns:
        entities of the type entityType based on attribute and values
        Throws:
        DatabaseException - in case of a database exception
      • select

        <T> List<Entity> select​(Attribute<T> attribute,
                                Collection<T> values)
                         throws DatabaseException
        Selects entities based on a single attribute condition, using values OR'ed together
        Type Parameters:
        T - the value type
        Parameters:
        attribute - the condition attribute
        values - the values to use as condition
        Returns:
        entities of the type entityType based on attribute and values
        Throws:
        DatabaseException - in case of a database exception
      • rowCount

        int rowCount​(Condition condition)
              throws DatabaseException
        Selects the number of rows returned based on the given condition
        Parameters:
        condition - the search condition
        Returns:
        the number of rows fitting the given condition
        Throws:
        DatabaseException - in case of a database exception
      • fillReport

        <T,​R,​P> R fillReport​(ReportType<T,​R,​P> reportType,
                                         P reportParameters)
                                  throws DatabaseException,
                                         ReportException
        Takes a ReportType object using a JDBC datasource and returns an initialized report result object
        Type Parameters:
        T - the report type
        R - the report result type
        P - the report parameters type
        Parameters:
        reportType - the report to fill
        reportParameters - the report parameters, if any
        Returns:
        the filled result object
        Throws:
        DatabaseException - in case of a database exception
        ReportException - in case of a report exception
        See Also:
        Report.fillReport(java.sql.Connection, Object)
      • writeBlob

        void writeBlob​(Key primaryKey,
                       Attribute<byte[]> blobAttribute,
                       byte[] blobData)
                throws DatabaseException
        Writes blobData in the blob field specified by the property identified by attribute for the given entity
        Parameters:
        primaryKey - the primary key of the entity for which to write the blob field
        blobAttribute - the blob attribute
        blobData - the blob data
        Throws:
        UpdateException - in case multiple rows were affected
        DatabaseException - in case of a database exception
      • readBlob

        byte[] readBlob​(Key primaryKey,
                        Attribute<byte[]> blobAttribute)
                 throws DatabaseException
        Reads the blob specified by the property identified by attribute from the given entity, returns null if no blob data is found.
        Parameters:
        primaryKey - the primary key of the entity
        blobAttribute - the blob attribute
        Returns:
        a byte array containing the blob data or null if no blob data is found
        Throws:
        DatabaseException - in case of a database exception