Class Database


  • public class Database
    extends java.lang.Object
    Database link for subsequent CRUD operations
    • Constructor Summary

      Constructors 
      Constructor Description
      Database​(DatabaseConnectionFactory connectionFactory)
      Create a database via custom connection factory
      Database​(java.lang.String jndiName)
      Create database object via JNDI handle
      Database​(java.lang.String driverName, java.lang.String jdbcUrl, java.lang.String username, java.lang.String password)
      Create database with simple non-pooled connections
      Database​(javax.sql.DataSource dataSource)
      Create database object via datasource
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> java.util.List<T> bulkInsert​(java.util.List<T> entities)
      Insert a collection of entities into database as a batch.
      boolean delete​(java.lang.Class<?> entityClass, java.lang.Object id)
      Delete an existing record
      int deleteWhere​(java.lang.Class<?> entityClass, java.lang.String whereExpression, java.lang.Object... whereParameters)
      Delete multiple records
      <T> T getById​(java.lang.Class<? extends T> entityClass, java.lang.Object id)
      Fetch a single record/entity from a database table
      Dialect getDialect()  
      <T> T insert​(T entity)
      Insert a single entity into database
      <T> java.util.List<T> listAll​(java.lang.Class<? extends T> entityClass)
      Fetch all records/entities from a database table into a list
      SqlQuery sql​(java.lang.String sqlSelect, java.lang.Object... whereParameters)
      Prepare a SELECT query
      <T> T transaction​(TransactionStatements<T> statements)
      Runs a bunch of statements in a single transaction
      void update​(java.lang.Object entity)
      Update an existing entity in database.
      void update​(java.lang.Object entity, java.lang.String whereExpression, java.lang.Object... whereParameters)
      Update an existing entity in database.
      SqlQuery where​(java.lang.String whereExpression, java.lang.Object... whereParameters)
      Prepare a SELECT query with WHERE filter only.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Database

        public Database​(javax.sql.DataSource dataSource)
        Create database object via datasource
        Parameters:
        dataSource - data source, which provides database connections
      • Database

        public Database​(DatabaseConnectionFactory connectionFactory)
        Create a database via custom connection factory
        Parameters:
        connectionFactory - custom connection factory, which provides database connections
      • Database

        public Database​(java.lang.String jndiName)
                 throws javax.naming.NamingException
        Create database object via JNDI handle
        Parameters:
        jndiName - JNDI name, for example "jdbc/demoDB"
        Throws:
        javax.naming.NamingException - when JNDI name lookup fails
      • Database

        public Database​(java.lang.String driverName,
                        java.lang.String jdbcUrl,
                        java.lang.String username,
                        java.lang.String password)
                 throws java.lang.Exception
        Create database with simple non-pooled connections
        Parameters:
        driverName - driver class name, for example "org.postgresql.Driver"
        jdbcUrl - database URL, for example "jdbc:postgresql://localhost:5432/demoDB"
        username - SQL username
        password - SQL password
        Throws:
        java.lang.Exception - when anything goes wrong
    • Method Detail

      • insert

        public <T> T insert​(T entity)
                     throws java.lang.Exception
        Insert a single entity into database
        Type Parameters:
        T - entity type
        Parameters:
        entity - entity to insert
        Returns:
        the same entity, with @Id field (if any) being initialized
        Throws:
        java.lang.Exception - when anything goes wrong
      • bulkInsert

        public <T> java.util.List<T> bulkInsert​(java.util.List<T> entities)
                                         throws java.lang.Exception
        Insert a collection of entities into database as a batch. Batch insertion is faster than inserting one by one.
        Type Parameters:
        T - entity type
        Parameters:
        entities - collection of entities to insert (currently there is a limit on records count that depends on the SQL driver)
        Returns:
        the same entities, with @Id field (if any) being initialized
        Throws:
        java.lang.Exception - when anything goes wrong
      • update

        public void update​(java.lang.Object entity)
                    throws java.lang.Exception
        Update an existing entity in database. Only entities with @Id field can be updated. When @Id field is missing, use method update(entity, whereExpression, whereParameters)
        Parameters:
        entity - entity with new attribute values
        Throws:
        java.lang.Exception - when anything goes wrong
      • update

        public void update​(java.lang.Object entity,
                           java.lang.String whereExpression,
                           java.lang.Object... whereParameters)
                    throws java.lang.Exception
        Update an existing entity in database. If WHERE expression selects multiple records, then all these records will be updated with new attribute values (except the @Id field).
        Parameters:
        entity - entity with new attribute values
        whereExpression - SQL WHERE expression, for example "name LIKE ?"
        whereParameters - parameters for WHERE expression
        Throws:
        java.lang.Exception - when anything goes wrong
      • delete

        public boolean delete​(java.lang.Class<?> entityClass,
                              java.lang.Object id)
                       throws java.lang.Exception
        Delete an existing record
        Parameters:
        entityClass - entity class, which indirectly refers to a database table
        id - entity id
        Returns:
        true, if the record existed before deletion; false, if the record did not exist
        Throws:
        java.lang.Exception - when anything goes wrong
      • deleteWhere

        public int deleteWhere​(java.lang.Class<?> entityClass,
                               java.lang.String whereExpression,
                               java.lang.Object... whereParameters)
                        throws java.lang.Exception
        Delete multiple records
        Parameters:
        entityClass - entity class, which indirectly refers to a database table
        whereExpression - SQL WHERE expression, for example "name LIKE ?"
        whereParameters - parameters for WHERE expression
        Returns:
        number of records deleted
        Throws:
        java.lang.Exception - when anything goes wrong
      • sql

        public SqlQuery sql​(java.lang.String sqlSelect,
                            java.lang.Object... whereParameters)
                     throws java.lang.Exception
        Prepare a SELECT query
        Parameters:
        sqlSelect - SQL SELECT statement
        whereParameters - parameter values for WHERE expression
        Returns:
        query object for fetching the results
        Throws:
        java.lang.Exception - when anything goes wrong
      • where

        public SqlQuery where​(java.lang.String whereExpression,
                              java.lang.Object... whereParameters)
                       throws java.lang.Exception
        Prepare a SELECT query with WHERE filter only. Subsequent fetch operation (get or list) determines database table for this query
        Parameters:
        whereExpression - SQL WHERE expression, for example "name LIKE ?"
        whereParameters - parameter values for WHERE expression
        Returns:
        query object for fetching the results
        Throws:
        java.lang.Exception - when anything goes wrong
      • listAll

        public <T> java.util.List<T> listAll​(java.lang.Class<? extends T> entityClass)
                                      throws java.lang.Exception
        Fetch all records/entities from a database table into a list
        Type Parameters:
        T - entity type
        Parameters:
        entityClass - entity class, which indirectly refers to a database table
        Returns:
        list of entities
        Throws:
        java.lang.Exception - when anything goes wrong
      • getById

        public <T> T getById​(java.lang.Class<? extends T> entityClass,
                             java.lang.Object id)
                      throws java.lang.Exception
        Fetch a single record/entity from a database table
        Type Parameters:
        T - entity type
        Parameters:
        entityClass - entity class, which indirectly refers to a database table
        id - entity id
        Returns:
        entity; returns null, if id refers to non-existing record
        Throws:
        java.lang.Exception - when anything goes wrong
      • transaction

        public <T> T transaction​(TransactionStatements<T> statements)
                          throws java.lang.Exception
        Runs a bunch of statements in a single transaction
        Type Parameters:
        T - entity type
        Parameters:
        statements - statements to run
        Returns:
        the return value from statements
        Throws:
        java.lang.Exception - when anything goes wrong
      • getDialect

        public Dialect getDialect()