public class DAO extends Object
| Constructor and Description |
|---|
DAO() |
DAO(DataSource ds) |
| Modifier and Type | Method and Description |
|---|---|
void |
createTables(List<Class<? extends Record>> tables)
This method creates table for the specified classes according to their annotations.
|
void |
delete(Integer id,
Class<? extends Record> clazz)
Deletes record from the database.
|
void |
delete(Record record)
Deleted record from the database.
|
Integer |
generatePrimaryKey(Class<? extends Record> clazz)
Returns next available primary key value.
|
<T extends Record> |
get(Integer id,
Class<? extends T> clazz)
Retrieves record from the database using record ID.
|
<T extends Record> |
getAll(Class<T> clazz)
Retrieves all records of the specified type.
|
<T extends Record> |
getAll(Class<T> clazz,
Map<Integer,T> result)
Retrieves all records of the specified type and fills the map.
|
Connection |
getConnection()
Returns connection for the current data source.
|
DataSource |
getDataSource()
Return current data source object.
|
<T extends Record> |
insert(int size,
List<T> records)
This method inserts multiple records with predefined id using batch insert.
|
<T extends Record> |
insert(T record)
This method inserts new record with predefined id into the database.
|
void |
preload(Collection<Class<? extends Record>> tables)
Pre-loads necessary information from the just opened database.
|
void |
setDataSource(DataSource ds)
Sets a new data source.
|
<T extends Record> |
update(T record)
Updates record in the database.
|
public DAO()
public DAO(DataSource ds)
public DataSource getDataSource()
public void setDataSource(DataSource ds)
ds - data sourcepublic Connection getConnection() throws SQLException
SQLException - in case of SQL errorpublic <T extends Record> T get(Integer id, Class<? extends T> clazz)
T - type of the recordid - record idclazz - record classpublic <T extends Record> List<T> getAll(Class<T> clazz)
T - type of the recordclazz - record classpublic <T extends Record> void getAll(Class<T> clazz, Map<Integer,T> result)
T - type of the recordclazz - record classresult - map to fillpublic void createTables(List<Class<? extends Record>> tables)
tables - list of tablespublic void preload(Collection<Class<? extends Record>> tables)
tables - list of Record typespublic Integer generatePrimaryKey(Class<? extends Record> clazz)
clazz - record classpublic <T extends Record> T insert(T record)
T - type of the recordrecord - recordpublic <T extends Record> void insert(int size, 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.
T - type of recordssize - size of the batchrecords - list of recordspublic <T extends Record> T update(T record)
Record, i.e. supplied object is
not changed.T - record typerecord - recordpublic void delete(Record record)
record - record to deleteCopyright © 2017, Petr Panteleyev