com.j256.ormlite.dao
Class BaseJdbcDao<T,ID>

java.lang.Object
  extended by com.j256.ormlite.support.SimpleDaoSupport
      extended by com.j256.ormlite.dao.BaseJdbcDao<T,ID>
All Implemented Interfaces:
CloseableIterable<T>, Dao<T,ID>, Iterable<T>

public abstract class BaseJdbcDao<T,ID>
extends SimpleDaoSupport
implements Dao<T,ID>

Base class for the Database Access Objects that handle the reading and writing a class from the database. This is the JDBC implementation of the Dao and extends Spring's SimpleDaoSupport. Kudos to Robert A. for the general ideas of this hierarchy.

This class is also Iterable which means you can do a for (T obj : dao) type of loop code to iterate through the table of persisted objects. See iterator().

NOTE: If you are using the Spring type wiring in Java, initialize() should be called after all of the set methods. In Spring XML, init-method="initialize" should be used.

Author:
graywatson

Field Summary
 
Fields inherited from class com.j256.ormlite.support.SimpleDaoSupport
databaseAccess, databaseType
 
Constructor Summary
protected BaseJdbcDao(Class<T> dataClass)
          Construct our base Jdbc class.
protected BaseJdbcDao(DatabaseTableConfig<T> tableConfig)
          Construct our base Jdbc class.
protected BaseJdbcDao(DatabaseType databaseType, Class<T> dataClass)
          Construct our base Jdbc class.
protected BaseJdbcDao(DatabaseType databaseType, DatabaseTableConfig<T> tableConfig)
          Construct our base Jdbc class.
 
Method Summary
 int create(T data)
          Create a new row in the database from an object.
static
<T,ID> Dao<T,ID>
createDao(DatabaseType databaseType, DataSource dataSource, Class<T> clazz)
          Helper method to create a Dao object without having to define a class.
 int delete(Collection<T> datas)
          Delete a collection of objects from the database using an IN SQL clause.
 int delete(T data)
          Delete an object from the database.
 int deleteIds(Collection<ID> ids)
          Delete the objects that match the collection of ids from the database using an IN SQL clause.
 Class<T> getDataClass()
          Returns the class associated with this DAO.
 DatabaseTableConfig<T> getTableConfig()
          Returns the table configuration information associated with the Dao's class.
 void initialize()
          Initialize the various DAO configurations.
 SelectIterator<T,ID> iterator()
          This satisfies the Iterable interface for the class and allows you to iterate through the objects in the table using SQL.
 SelectIterator<T,ID> iterator(PreparedQuery<T> preparedQuery)
          Same as Dao.iterator() but with a PreparedQuery parameter.
 RawResults iteratorRaw(String query)
          Same as Dao.iterator(PreparedQuery) except it returns a RawResults object associated with the SQL select query argument.
 boolean objectsEqual(T data1, T data2)
          Return true if the two arguments are equal.
 String objectToString(T data)
          Return the string version of the object with each of the known field values shown.
 List<T> query(PreparedQuery<T> preparedQuery)
          Query for the items in the object table which match the PreparedQuery.
 QueryBuilder<T,ID> queryBuilder()
          Create and return a new QueryBuilder object which allows you to build a custom query.
 List<T> queryForAll()
          Query for all of the items in the object table.
 RawResults queryForAllRaw(String queryString)
          Query for all of the items in the object table that match the SQL select query argument.
 T queryForFirst(PreparedQuery<T> preparedQuery)
          Query for and return the first item in the object table which matches the PreparedQuery.
 T queryForId(ID id)
          Retrieves an object associated with a specific ID.
 int refresh(T data)
          Does a query for the object's id and copies in each of the field values from the database to refresh the data parameter.
 void setTableConfig(DatabaseTableConfig<T> tableConfig)
          Used if you want to configure the class for the Dao by hand or with spring instead of using the DatabaseField annotation in the class.
 int update(T data)
          Save the fields from an object to the database.
 int updateId(T data, ID newId)
          Update an object in the database to change its id to the newId parameter.
 
Methods inherited from class com.j256.ormlite.support.SimpleDaoSupport
setDatabaseType, setDataSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BaseJdbcDao

protected BaseJdbcDao(Class<T> dataClass)
Construct our base Jdbc class. The DatabaseType must be set with the SimpleDaoSupport.setDatabaseType(com.j256.ormlite.db.DatabaseType) method before initialize() method is called. The dataClass provided must have its fields marked with DatabaseField annotations or the setTableConfig(com.j256.ormlite.table.DatabaseTableConfig) method must be called before the initialize() method is called.

Parameters:
dataClass - Class associated with this Dao. This must match the T class parameter.

BaseJdbcDao

protected BaseJdbcDao(DatabaseType databaseType,
                      Class<T> dataClass)
Construct our base Jdbc class. The dataClass provided must have its fields marked with DatabaseField annotations or the setTableConfig(com.j256.ormlite.table.DatabaseTableConfig) method must be called before the initialize() method is called.

Parameters:
databaseType - Type of database.
dataClass - Class associated with this Dao. This must match the T class parameter.

BaseJdbcDao

protected BaseJdbcDao(DatabaseTableConfig<T> tableConfig)
Construct our base Jdbc class. The DatabaseType must be set with the SimpleDaoSupport.setDatabaseType(com.j256.ormlite.db.DatabaseType) method before initialize() method is called.

Parameters:
tableConfig - Hand or spring wired table configuration information.

BaseJdbcDao

protected BaseJdbcDao(DatabaseType databaseType,
                      DatabaseTableConfig<T> tableConfig)
Construct our base Jdbc class.

Parameters:
databaseType - Type of database.
tableConfig - Hand or spring wired table configuration information.
Method Detail

initialize

public void initialize()
                throws SQLException
Initialize the various DAO configurations. This method is called from initialize() which must be called after the Dao is configured (done by Spring automagically). This method should not be called directly by the Ormlite user.

Overrides:
initialize in class SimpleDaoSupport
Throws:
SQLException

queryForId

public T queryForId(ID id)
             throws SQLException
Description copied from interface: Dao
Retrieves an object associated with a specific ID.

Specified by:
queryForId in interface Dao<T,ID>
Parameters:
id - Identifier that matches a specific row in the database to find and return.
Returns:
The object that has the ID field which equals id or null if no matches.
Throws:
SQLException - on any SQL problems or if more than 1 item with the id are found in the database.

queryForFirst

public T queryForFirst(PreparedQuery<T> preparedQuery)
                throws SQLException
Description copied from interface: Dao
Query for and return the first item in the object table which matches the PreparedQuery. See Dao.queryBuilder() for more information. This can be used to return the object that matches a single unique column. You should use Dao.queryForId(ID) if you want to query for the id column.

Specified by:
queryForFirst in interface Dao<T,ID>
Parameters:
preparedQuery - Query used to match the objects in the database.
Returns:
The first object that matches the query.
Throws:
SQLException - on any SQL problems.

queryForAll

public List<T> queryForAll()
                    throws SQLException
Description copied from interface: Dao
Query for all of the items in the object table. For medium sized or large tables, this may load a lot of objects into memory so you should consider using the Dao.iterator() method instead.

Specified by:
queryForAll in interface Dao<T,ID>
Returns:
A list of all of the objects in the table.
Throws:
SQLException - on any SQL problems.

queryBuilder

public QueryBuilder<T,ID> queryBuilder()
Description copied from interface: Dao
Create and return a new QueryBuilder object which allows you to build a custom query. You call methods on the QueryBuilder to construct your custom query and then call QueryBuilder.prepareQuery() once you are ready to build your query. This returns a PreparedQuery object which gets passed to Dao.query(PreparedQuery) or Dao.iterator(PreparedQuery).

Specified by:
queryBuilder in interface Dao<T,ID>

query

public List<T> query(PreparedQuery<T> preparedQuery)
              throws SQLException
Description copied from interface: Dao
Query for the items in the object table which match the PreparedQuery. See Dao.queryBuilder() for more information.

NOTE: For medium sized or large tables, this may load a lot of objects into memory so you should consider using the Dao.iterator(PreparedQuery) method instead.

Specified by:
query in interface Dao<T,ID>
Parameters:
preparedQuery - Query used to match the objects in the database.
Returns:
A list of all of the objects in the table that match the query.
Throws:
SQLException - on any SQL problems.

queryForAllRaw

public RawResults queryForAllRaw(String queryString)
                          throws SQLException
Description copied from interface: Dao
Query for all of the items in the object table that match the SQL select query argument. Although you should use the QueryBuilder for most queries, this method allows you to do special queries that aren't supported otherwise. For medium sized or large tables, this may load a lot of objects into memory so you should consider using the Dao.iteratorRaw(String) method instead.

Specified by:
queryForAllRaw in interface Dao<T,ID>
Returns:
A raw results object from which you can get the results.
Throws:
SQLException - on any SQL problems.

create

public int create(T data)
           throws SQLException
Description copied from interface: Dao
Create a new row in the database from an object.

Specified by:
create in interface Dao<T,ID>
Parameters:
data - The data item that we are creating in the database.
Returns:
The number of rows updated in the database. This should be 1.
Throws:
SQLException

update

public int update(T data)
           throws SQLException
Description copied from interface: Dao
Save the fields from an object to the database. If you have made changes to an object, this is how you persist those changes to the database. You cannot use this method to update the id field -- see Dao.updateId(T, ID).

Specified by:
update in interface Dao<T,ID>
Parameters:
data - The data item that we are updating in the database.
Returns:
The number of rows updated in the database. This should be 1.
Throws:
SQLException - on any SQL problems.

updateId

public int updateId(T data,
                    ID newId)
             throws SQLException
Description copied from interface: Dao
Update an object in the database to change its id to the newId parameter. The data must have its current id set. If the id field has already changed then it cannot be updated. After the id has been updated in the database, the id field of the data object will also be changed.

NOTE: Depending on the database type and the id type, you may be unable to change the id of the field.

Specified by:
updateId in interface Dao<T,ID>
Parameters:
data - The data item that we are updating in the database with the current id.
newId - The new id that you want to update the data with.
Returns:
The number of rows updated in the database. This should be 1.
Throws:
SQLException - on any SQL problems.

refresh

public int refresh(T data)
            throws SQLException
Description copied from interface: Dao
Does a query for the object's id and copies in each of the field values from the database to refresh the data parameter. Any local object changes to persisted fields will be overwritten. If the database has been updated this brings your local object up to date.

Specified by:
refresh in interface Dao<T,ID>
Parameters:
data - The data item that we are refreshing with fields from the database.
Returns:
The number of rows found in the database that correspond to the data id. This should be 1.
Throws:
SQLException - on any SQL problems or if the data item is not found in the table or if more than 1 item is found with data's id.

delete

public int delete(T data)
           throws SQLException
Description copied from interface: Dao
Delete an object from the database.

Specified by:
delete in interface Dao<T,ID>
Parameters:
data - The data item that we are deleting from the database.
Returns:
The number of rows updated in the database. This should be 1.
Throws:
SQLException - on any SQL problems.

delete

public int delete(Collection<T> datas)
           throws SQLException
Description copied from interface: Dao
Delete a collection of objects from the database using an IN SQL clause.

Specified by:
delete in interface Dao<T,ID>
Parameters:
datas - A collection of data items to be deleted.
Returns:
The number of rows updated in the database. This should be the size() of the collection.
Throws:
SQLException - on any SQL problems.

deleteIds

public int deleteIds(Collection<ID> ids)
              throws SQLException
Description copied from interface: Dao
Delete the objects that match the collection of ids from the database using an IN SQL clause.

Specified by:
deleteIds in interface Dao<T,ID>
Parameters:
ids - A collection of data ids to be deleted.
Returns:
The number of rows updated in the database. This should be the size() of the collection.
Throws:
SQLException - on any SQL problems.

iterator

public SelectIterator<T,ID> iterator()
Description copied from interface: Dao
This satisfies the Iterable interface for the class and allows you to iterate through the objects in the table using SQL. You can use code similar to the following:

 for (Account account : accountDao) { ... }
 

WARNING: because the Iterator.hasNext(), Iterator.next(), etc. methods can only throw RuntimeException, the code has to wrap any SQLException with IllegalStateException. Make sure to catch IllegalStateException and look for a SQLException cause. See the SelectIterator source code for more details.

WARNING: The underlying results object will only be closed if you page all the way to the end of the iterator using the for() loop or if you call SelectIterator.close() directly. It is also closed when it is garbage collected but this is considered bad form.

Specified by:
iterator in interface CloseableIterable<T>
Specified by:
iterator in interface Dao<T,ID>
Specified by:
iterator in interface Iterable<T>
Returns:
An iterator of the class that uses SQL to step across the database table.

iterator

public SelectIterator<T,ID> iterator(PreparedQuery<T> preparedQuery)
                              throws SQLException
Description copied from interface: Dao
Same as Dao.iterator() but with a PreparedQuery parameter. See Dao.queryBuilder() for more information. You use it like the following:

 QueryBuilder<Account, String> qb = accountDao.queryBuilder();
 ... custom query builder methods
 SelectIterator<Account> iterator = partialDao.iterator(qb.prepareQuery());
 try {
     while (iterator.hasNext()) {
         Account account = iterator.next();
         ...
     }
 } finish {
     iterator.close();
 }
 

Specified by:
iterator in interface Dao<T,ID>
Parameters:
preparedQuery - Query used to iterate across a sub-set of the items in the database.
Returns:
An iterator for T.
Throws:
SQLException - on any SQL problems.

iteratorRaw

public RawResults iteratorRaw(String query)
                       throws SQLException
Description copied from interface: Dao
Same as Dao.iterator(PreparedQuery) except it returns a RawResults object associated with the SQL select query argument. Although you should use the Dao.iterator() for most queries, this method allows you to do special queries that aren't supported otherwise. Like the above iterator methods, you must call close on the returned RawResults object once you are done with it.

Specified by:
iteratorRaw in interface Dao<T,ID>
Throws:
SQLException

objectToString

public String objectToString(T data)
Description copied from interface: Dao
Return the string version of the object with each of the known field values shown. Useful for testing and debugging.

Specified by:
objectToString in interface Dao<T,ID>
Parameters:
data - The data item for which we are returning the toString information.

objectsEqual

public boolean objectsEqual(T data1,
                            T data2)
                     throws SQLException
Description copied from interface: Dao
Return true if the two arguments are equal. This checks each of the fields defined in the database to see if they are equal. Useful for testing and debugging.

Specified by:
objectsEqual in interface Dao<T,ID>
Parameters:
data1 - One of the data items that we are checking for equality.
data2 - The other data item that we are checking for equality.
Throws:
SQLException

getDataClass

public Class<T> getDataClass()
Returns the class associated with this DAO.


getTableConfig

public DatabaseTableConfig<T> getTableConfig()
Returns the table configuration information associated with the Dao's class.


setTableConfig

public void setTableConfig(DatabaseTableConfig<T> tableConfig)
Used if you want to configure the class for the Dao by hand or with spring instead of using the DatabaseField annotation in the class. This must be called before initialize().


createDao

public static <T,ID> Dao<T,ID> createDao(DatabaseType databaseType,
                                         DataSource dataSource,
                                         Class<T> clazz)
                           throws SQLException
Helper method to create a Dao object without having to define a class. Dao classes are supposed to be convenient but if you have a lot of classes, they can seem to be a pain.

Throws:
SQLException


Copyright © 2010. All Rights Reserved.