public abstract class AbstrIdDAO<T> extends AbstrBasicDAO<T> implements IIdDAO<T>
DAO framework class covering basic database-operations.
This class implicitly support two ID policies:
Database-generated keys work fine for mysql, but may have problems with other database engines. For utmost database-independence, UUID-based IDs are recommended.
Since version 1.1 of sql-util you may provide for an ID generator for
Long
IDs in order to generate unique IDs for all your entities and to
improve database-independence.
Constructor and Description |
---|
AbstrIdDAO(Class<T> clazz,
String idProperty,
String tableName,
String... columns)
Default Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
delete(Serializable id)
Remove DTO for the given id.
|
int[] |
deleteBatch(Collection<? extends Serializable> ids) |
protected String |
generateUID()
Convenience method which generates a unique Id for this object.
|
T |
get(Serializable id)
Get an Object for the given id or
null if no such DTO exists. |
List<T> |
getBatch(Collection<? extends Serializable> ids)
Returns a List containing all Objects for the given ids.
|
List<T> |
getBatch(Serializable... ids)
Returns a List containing all DTOs for the given ids.
|
Class<T> |
getEntityClass() |
Serializable |
getId(T dto) |
Class<? extends Serializable> |
getIdClass() |
IdGenerator |
getIdGenerator() |
String |
getPkColumn() |
protected SQLValue |
getSQLValueForId(Serializable id) |
T |
save(T dto)
Save the given DTO an generate a new ID.
|
List<T> |
saveBatch(List<T> dtos)
Save the given list of DTOs in a database batch and generate
new IDs for them.
|
void |
setIdGenerator(IdGenerator idGenerator) |
protected void |
setIdOnStatement(PreparedStatement statement,
int pos,
Object id) |
void |
setIdProvided(boolean idProvided) |
int |
update(T dto) |
int[] |
updateBatch(Collection<T> dtos)
Update a collection of objects as an SQL batch.
|
deleteWithCondition, fillDtoFromResultSet, fillPreparedStatementFromDto, getAll, getColumnCount, getColumnName, getColumnNames, getGenerator, getListSize, getListWithCondition, getSubList, getTableName, getUniqueWithCondition, setGenerator, streamListWithCondition, streamSubList
closeStatement, getConnection, getThreadLocalKey, performWithPreparedStatement, performWithPreparedStatement, performWithPreparedStatement, performWithPreparedStatement, performWithPreparedStatement, performWithStatement, performWithStatement, setThreadLocalKey
public AbstrIdDAO(Class<T> clazz, String idProperty, String tableName, String... columns)
clazz
- The class object of T
used to retrieved the getter for the id property.idProperty
- The name of the ID property. The according column may be named differently, it is specified as first column in columns
and can be checked using getPkColumn()
.tableName
- Mandatory. The name of the table for which this DAO is used.columns
- Mandatory list of column names of the table for which this DAO is used. Is expected to begin with
the primary key column which holds the id.AbstrBasicDAO
public Serializable getId(T dto)
public Class<? extends Serializable> getIdClass()
getIdClass
in interface IIdDAO<T>
protected void setIdOnStatement(PreparedStatement statement, int pos, Object id) throws SQLException
SQLException
public T save(T dto)
IBasicDAO
public List<T> saveBatch(List<T> dtos)
IBasicDAO
public int update(T dto)
public int[] updateBatch(Collection<T> dtos)
IBasicDAO
UnsupportedOperationException
in the default
implementation.updateBatch
in interface IBasicDAO<T>
updateBatch
in class AbstrBasicDAO<T>
dtos
- A collection of DTOs to updatedtos.size()
update counts per
dto to be updated.public T get(Serializable id)
null
if no such DTO exists.get
in interface IIdDAO<T>
id
- The id for the queried DTOnull
SQLException
public List<T> getBatch(Serializable... ids)
public List<T> getBatch(Collection<? extends Serializable> ids)
protected SQLValue getSQLValueForId(Serializable id)
public boolean delete(Serializable id)
delete
in interface IIdDAO<T>
id
- SQLException
public int[] deleteBatch(Collection<? extends Serializable> ids)
deleteBatch
in interface IIdDAO<T>
ids
- A collection of IDs of objects to delete.ids.size()
counts
of objects really deleted from the database,
which contains 0 or 1 for each dto to be deleted.protected String generateUID()
UUID
).public String getPkColumn()
public IdGenerator getIdGenerator()
Long
, which
disables the use of database-generated IDs.public void setIdGenerator(IdGenerator idGenerator)
idGenerator
- An ID generator to use for IDs
of type Long
, which
disables the use of database-generated IDs.public void setIdProvided(boolean idProvided)
Copyright © 2017 Clazzes.org. All rights reserved.