Class DAO



  • public class DAO
    extends java.lang.Object
    Persistence API entry point.
    • Constructor Summary

      Constructors 
      Constructor Description
      DAO​()  
      DAO​(javax.sql.DataSource ds)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void createTables​(java.util.List<java.lang.Class<? extends Record>> tables)
      This method creates table for the specified classes according to their annotations.
      void delete​(java.lang.Integer id, java.lang.Class<? extends Record> clazz)
      Deletes record from the database.
      void delete​(Record record)
      Deleted record from the database.
      void deleteAll​(java.lang.Class<? extends Record> table)
      Deletes all records from table.
      void deleteAll​(java.sql.Connection connection, java.lang.Class<? extends Record> table)
      Deletes all records from table using provided connection.
      void dropTables​(java.util.List<java.lang.Class<? extends Record>> tables)
      Drops specified tables according to their annotations.
      java.lang.Integer generatePrimaryKey​(java.lang.Class<? extends Record> clazz)
      Returns next available primary key value.
      <T extends Record>
      T
      get​(java.lang.Integer id, java.lang.Class<? extends T> clazz)
      Retrieves record from the database using record ID.
      <T extends Record>
      java.util.List<T>
      getAll​(java.lang.Class<T> clazz)
      Retrieves all records of the specified type.
      <T extends Record>
      void
      getAll​(java.lang.Class<T> clazz, java.util.Map<java.lang.Integer,T> result)
      Retrieves all records of the specified type and fills the map.
      java.sql.Connection getConnection​()
      Returns connection for the current data source.
      javax.sql.DataSource getDataSource​()
      Return current data source object.
      <T extends Record>
      void
      insert​(int size, java.util.List<T> records)
      This method inserts multiple records with predefined id using batch insert.
      <T extends Record>
      void
      insert​(java.sql.Connection conn, int size, java.util.List<T> records)
      This method inserts multiple records with predefined id using batch insert.
      <T extends Record>
      T
      insert​(java.sql.Connection conn, T record)
      This method inserts new record with predefined id into the database.
      <T extends Record>
      T
      insert​(T record)
      This method inserts new record with predefined id into the database.
      void preload​(java.util.Collection<java.lang.Class<? extends Record>> tables)
      Pre-loads necessary information from the just opened database.
      protected void resetPrimaryKey​(java.lang.Class<? extends Record> table)
      Resets primary key generation for the given table.
      void setDataSource​(javax.sql.DataSource ds)
      Sets a new data source.
      void truncate​(java.util.List<java.lang.Class<? extends Record>> tables)
      Truncates tables removing all records.
      <T extends Record>
      T
      update​(java.sql.Connection conn, T record)
      Updates record in the database.
      <T extends Record>
      T
      update​(T record)
      Updates record in the database.
      • Methods inherited from class java.lang.Object

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

      • DAO

        public DAO​()
      • DAO

        public DAO​(javax.sql.DataSource ds)
    • Method Detail

      • getDataSource

        public javax.sql.DataSource getDataSource​()
        Return current data source object.
        Returns:
        data source object
      • setDataSource

        public void setDataSource​(javax.sql.DataSource ds)
        Sets a new data source.
        Parameters:
        ds - data source
      • getConnection

        public java.sql.Connection getConnection​()
                                          throws java.sql.SQLException
        Returns connection for the current data source.
        Returns:
        connection
        Throws:
        java.sql.SQLException - in case of SQL error
      • get

        public <T extends Record> T get​(java.lang.Integer id,
                                        java.lang.Class<? extends T> clazz)
        Retrieves record from the database using record ID.
        Type Parameters:
        T - type of the record
        Parameters:
        id - record id
        clazz - record class
        Returns:
        record
      • getAll

        public <T extends Record> java.util.List<T> getAll​(java.lang.Class<T> clazz)
        Retrieves all records of the specified type.
        Type Parameters:
        T - type of the record
        Parameters:
        clazz - record class
        Returns:
        list of records
      • getAll

        public <T extends Record> void getAll​(java.lang.Class<T> clazz,
                                              java.util.Map<java.lang.Integer,T> result)
        Retrieves all records of the specified type and fills the map.
        Type Parameters:
        T - type of the record
        Parameters:
        clazz - record class
        result - map to fill
      • createTables

        public void createTables​(java.util.List<java.lang.Class<? extends Record>> tables)
        This method creates table for the specified classes according to their annotations.
        Parameters:
        tables - list of tables
      • preload

        public void preload​(java.util.Collection<java.lang.Class<? extends Record>> tables)
        Pre-loads necessary information from the just opened database. This method must be called prior to any other database operations. Otherwise primary keys may be generated incorrectly.
        Parameters:
        tables - list of Record types
      • generatePrimaryKey

        public java.lang.Integer generatePrimaryKey​(java.lang.Class<? extends Record> clazz)
        Returns next available primary key value. This method is thread safe.
        Parameters:
        clazz - record class
        Returns:
        primary key value
      • insert

        public <T extends Record> T insert​(T record)
        This method inserts new record with predefined id into the database. No attempt to generate new id is made. Calling code must ensure that predefined id is unique.
        Type Parameters:
        T - type of the record
        Parameters:
        record - record
        Returns:
        inserted record
        Throws:
        java.lang.IllegalArgumentException - if id of the record is 0
      • insert

        public <T extends Record> T insert​(java.sql.Connection conn,
                                           T record)
        This method inserts new record with predefined id into the database. No attempt to generate new id is made. Calling code must ensure that predefined id is unique.
        Type Parameters:
        T - type of the record
        Parameters:
        conn - SQL connection
        record - record
        Returns:
        inserted record
        Throws:
        java.lang.IllegalArgumentException - if id of the record is 0
      • insert

        public <T extends Record> void insert​(int size,
                                              java.util.List<T> records)

        This method inserts multiple records with predefined id using batch insert. No attempt to generate new id is made. Calling code must ensure that predefined id is unique for all records.

        Supplied records are divided to batches of the specified size. To avoid memory issues size of the batch must be tuned appropriately.

        Type Parameters:
        T - type of records
        Parameters:
        size - size of the batch
        records - list of records
      • insert

        public <T extends Record> void insert​(java.sql.Connection conn,
                                              int size,
                                              java.util.List<T> records)

        This method inserts multiple records with predefined id using batch insert. No attempt to generate new id is made. Calling code must ensure that predefined id is unique for all records.

        Supplied records are divided to batches of the specified size. To avoid memory issues size of the batch must be tuned appropriately.

        Type Parameters:
        T - type of records
        Parameters:
        conn - SQL connection
        size - size of the batch
        records - list of records
      • update

        public <T extends Record> T update​(T record)
        Updates record in the database. This method returns instance of the Record, i.e. supplied object is not changed.
        Type Parameters:
        T - record type
        Parameters:
        record - record
        Returns:
        updated record
        Throws:
        java.lang.IllegalArgumentException - if id of the record is 0
      • update

        public <T extends Record> T update​(java.sql.Connection conn,
                                           T record)
        Updates record in the database. This method returns instance of the Record, i.e. supplied object is not changed.
        Type Parameters:
        T - record type
        Parameters:
        conn - SQL connection
        record - record
        Returns:
        updated record
        Throws:
        java.lang.IllegalArgumentException - if id of the record is 0
      • delete

        public void delete​(Record record)
        Deleted record from the database.
        Parameters:
        record - record to delete
      • delete

        public void delete​(java.lang.Integer id,
                           java.lang.Class<? extends Record> clazz)
        Deletes record from the database.
        Parameters:
        id - id of the record
        clazz - record type
      • deleteAll

        public void deleteAll​(java.lang.Class<? extends Record> table)
        Deletes all records from table.
        Parameters:
        table - table
      • deleteAll

        public void deleteAll​(java.sql.Connection connection,
                              java.lang.Class<? extends Record> table)
        Deletes all records from table using provided connection.
        Parameters:
        connection - SQL connection
        table - table class
      • truncate

        public void truncate​(java.util.List<java.lang.Class<? extends Record>> tables)
        Truncates tables removing all records. Primary key generation starts from 1 again. For MySQL this operation uses TRUNCATE TABLE table_name command. As SQLite does not support this command DELETE FROM table_name is used instead.
        Parameters:
        tables - tables to truncate
      • resetPrimaryKey

        protected void resetPrimaryKey​(java.lang.Class<? extends Record> table)
        Resets primary key generation for the given table. Next call to generatePrimaryKey(Class) will return 1. This method should only be used in case of manual table truncate.
        Parameters:
        table - table class
      • dropTables

        public void dropTables​(java.util.List<java.lang.Class<? extends Record>> tables)
        Drops specified tables according to their annotations.
        Parameters:
        tables - table classes