R
- type of row objectspublic class Table<R> extends java.lang.Object implements TypeTranslatorMap, TransactionListener
RowTranslator
for reading/writing
data form/to database and contains methods for common input/output operations. A RowTranslator
is created based upon fields within the row class of type R and from annotations for the
class. The primary key is defined by Column.primaryKey()
, Column.identity()
, or
Row.primaryKeyFields()
on class R.
Example 1 - Get table from database:
Connection connection = ... // jdbc connection Database database = new Database(connection); Table<MyRow> table = database.getTable(MyRow.class); table.selectAll();
Example 2 - Instantiate table:
Connection connection = ... // jdbc connection Database database = new Database(connection); Table<MyRow> table = new Table<MyRow>(database, MyRow.class); table.selectAll();
Example 3 - Instantiate a table subclass:
public class MyCustomTable extends Table<MyRow> { public MyCustomTable(Connection connection) { super(connection, MyRow.class); } ... } Connection connection = ... // jdbc connection Database database = new Database(connection); MyCustomTable table = new MyCustomTable(database); table.selectAll();
Since 4.1, related cascades are performed in batch mode for batch methods.
Constructor and Description |
---|
Table(Database database,
java.lang.Class<R> rowClass)
Constructs for a database and the class that used for row objects.
|
Modifier and Type | Method and Description |
---|---|
void |
begin(Transaction transaction)
TransactionListener.begin(Transaction) implementation. |
void |
commit(Transaction transaction)
TransactionListener.commit(Transaction) implementation. |
int |
delete(java.lang.Object... parameters)
Deletes by primary key.
|
int |
delete(R row)
Deletes by primary key.
|
int |
deleteAll()
Deletes all rows in table.
|
int |
deleteAll(java.util.Collection<R> rows)
Deletes many rows by primary key.
|
int |
deleteAllBatch(java.util.Collection<R> rows)
Deletes many rows by primary key in batch mode.
|
int |
deleteBatch(R row)
Deletes by primary key in batch mode.
|
void |
flush()
Writes uncommitted cache changes to database and then evicts all rows from cache.
|
Cache<R> |
getCache()
Gets the cache for this table.
|
Database |
getDatabase()
Gets the database supplied in constructor.
|
java.util.List<java.lang.reflect.Field> |
getLazySelectCascadeFields()
Gets fields for table record class that are annotated with
SelectCascade.lazy() true. |
java.util.List<? extends NameTranslator> |
getNameTranslators()
Gets the name translators used by
translateName(String) . |
java.lang.String |
getQualifiedTableName()
Gets table name used in sql statements with optional schema prefix if necessary.
|
java.lang.String[] |
getRequiredCascades()
Gets the required cascade names.
|
java.lang.Class<R> |
getRowClass() |
RowTranslator<R> |
getRowTranslator()
Gets the row translator for converting row values to/from sql parameters.
|
java.lang.String |
getTableName()
Gets the table name used in sql statements.
|
TypeTranslator<?> |
getTypeTranslator(java.lang.Class<?> typeClass)
Gets the translator to use to convert a value to a prepared statement or to
convert from a result set.
|
protected Cache<R> |
initCache()
Initialize cache for this table.
|
protected Cached |
initCachedAnnotation()
Gets the annotation that defines caching.
|
protected java.util.List<? extends NameTranslator> |
initNameTranslators(Row rowAnnotation)
Initialize all name translators annotated on table.
|
protected RowTranslator<R> |
initRowTranslator(Row rowAnnotation)
Creates a
RowTranslator for use by this table. |
protected java.lang.String |
initTableName(Row rowAnnotation)
Initializes table name from row annotation.
|
protected void |
initTypeTranslatorMap()
Initializes type translators for table.
|
int |
insert(R row)
Inserts one row into table.
|
int |
insertAll(java.util.Collection<R> rows)
Inserts collection of rows.
|
int |
insertAllBatch(java.util.Collection<R> rows)
Inserts a collection of rows in batch mode.
|
int |
insertBatch(R row)
Inserts row into table in batch mode.
|
boolean |
isAutoGeneratedKeys()
Gets the most recent value of
setAutoGeneratedKeys(boolean) . |
boolean |
isCached()
Indicates if table is cached.
|
boolean |
isReadOnly()
Gets read-only indicator.
|
R |
newRow()
Creates new instance of row.
|
void |
putTypeTranslator(java.lang.Class<?> typeClass,
TypeTranslator<?> typeTranslator)
Overrides translator defined in
Database for all operations on this table. |
void |
putTypeTranslator(java.lang.String typeClassName,
TypeTranslator<?> typeTranslator)
Same as
putTypeTranslator(Class, TypeTranslator) but uses class name. |
void |
rollback(Transaction transaction)
TransactionListener.rollback(Transaction) implementation. |
int |
save(R row)
Uses
SaveOperation to update an existing row or insert row if it
is not already in database. |
int |
saveAll(java.util.Collection<R> rows)
Uses
SaveOperation to update an existing rows or insert rows if they
are not already in database. |
int |
saveAllBatch(java.util.Collection<R> rows)
Saves a collection of rows in batch mode.
|
int |
saveBatch(R row)
Inserts or updates row in batch mode.
|
R |
select(java.lang.Object... primaryKeys)
Selects one row in table using primary key.
|
java.util.List<R> |
selectAll()
Selects all rows in table.
|
java.util.List<R> |
selectAllCustom(java.lang.String customSql,
java.lang.Object... parameters)
Select list of rows using custom sql.
|
java.util.List<R> |
selectAllWhere(java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects list of rows for where condition and parameters.
|
java.util.List<R> |
selectAllWhereOrdered(java.lang.String whereConditionName,
java.lang.String orderByName,
java.lang.Object... parameters)
Selects list of rows for where condition and parameters.
|
<T> T |
selectAvg(java.lang.String expression)
Selects average value.
|
<T> T |
selectAvg(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects average value.
|
int |
selectCount()
Gets count of all rows in table.
|
<T> T |
selectCount(java.lang.String expression)
Selects count of rows.
|
int |
selectCount(java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects count for a subset of rows.
|
<T> T |
selectCount(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects count of rows.
|
R |
selectCustom(java.lang.String customSql,
java.lang.Object... parameters)
Selects one row using custom sql.
|
<T> T |
selectMax(java.lang.String expression)
Selects maximum value.
|
<T> T |
selectMax(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects maximum value.
|
<T> T |
selectMin(java.lang.String expression)
Selects minimum value.
|
<T> T |
selectMin(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects minimum value.
|
<T> T |
selectSum(java.lang.String expression)
Selects sum.
|
<T> T |
selectSum(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects sum.
|
R |
selectWhere(java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects one row for where condition and parameters.
|
void |
setAutoGeneratedKeys(boolean autoGeneratedKeys)
Sets the default way identity columns are processed for operations that use this
Table . |
void |
setReadOnly(boolean readOnly)
Sets read-only indicator.
|
void |
setRequiredCascades(java.lang.String... cascadeNames)
Sets the name(s) of cascades that should occur with this operation.
|
void |
setTableName(java.lang.String tableName)
Sets the table name to use in sql statements.
|
java.lang.String |
translateName(java.lang.String javaName)
Converts a Java class or field name to corresponding SQL name.
|
int |
update(R row)
Updates one row in table by primary key.
|
int |
updateAll(java.util.Collection<R> rows)
Updates collection of rows using primary key.
|
int |
updateAllBatch(java.util.Collection<R> rows)
Updates collection of rows using primary key in batch mode.
|
int |
updateBatch(R row)
Updates row in table by primary key in batch mode.
|
public Table(Database database, java.lang.Class<R> rowClass) throws SormulaException
RowTranslator
for mapping row objects to/from database.
Since 4.1, setAutoGeneratedKeys(boolean)
defaults to Database.isAutoGeneratedKeys()
.
database
- database for this tablerowClass
- row objects are of this typeSormulaException
- if errorpublic boolean isAutoGeneratedKeys()
setAutoGeneratedKeys(boolean)
. The default is
Database.isAutoGeneratedKeys()
at the time of construction of this Table
.public void setAutoGeneratedKeys(boolean autoGeneratedKeys)
Table
.
When autoGeneratedKeys is false, any row field annotated as true for Column.identity()
will insert the value of the field instead of getting an auto generated key from the database.
Setting to false is useful when a column is defined as an identity column but the key will be manually created by the application.
autoGeneratedKeys
- true if identity columns get their value as automatically generated
by the database; false if database will generate the identity columnColumn.identity()
,
isAutoGeneratedKeys()
,
Statement.getGeneratedKeys()
public boolean isCached()
public Cache<R> getCache()
public void flush() throws SormulaException
SormulaException
- if errorpublic boolean isReadOnly()
public void setReadOnly(boolean readOnly)
ReadOnlyException
.
By default read-only is false unless row or table class is
annotated with Row.readOnly()
of true. Set to true as a safe-guard
to prevent accidental modification of table.readOnly
- true to prevent modify operationsRow.readOnly()
public void setRequiredCascades(java.lang.String... cascadeNames)
For all cascades that are executed, cascadeNames is passed on to the cascade operation so that all cascades for all levels use the same required cascade names.
The wildcard "*" parameter will result in StackOverflowError
if cascade relationships form
a cyclic graph and no termination condition exists to end the recursion.
If Database.getTable(Class)
is used, keep in mind that the names set with this method
will be in effect for all future uses of the same Table
object until changed.
cascadeNames
- name(s) of cascades to use; "*" for wildcard to use all cascadespublic java.lang.String[] getRequiredCascades()
protected void initTypeTranslatorMap() throws SormulaException
putTypeTranslator(Class, TypeTranslator)
.SormulaException
- if errorprotected java.util.List<? extends NameTranslator> initNameTranslators(Row rowAnnotation)
Table
may
contain NameTranslator
annotations instead of annotating row class.rowAnnotation
- the annotation that defines name translator(s)protected java.lang.String initTableName(Row rowAnnotation)
Row.tableName()
. If no name is supplied, then table name will
be row class simple name, Class.getSimpleName()
.rowAnnotation
- row annotation on table or null if noneprotected RowTranslator<R> initRowTranslator(Row rowAnnotation) throws TranslatorException
RowTranslator
for use by this table. Invoked by constructor.rowAnnotation
- annotation on table or row classTranslatorException
- if errorprotected Cache<R> initCache() throws CacheException
CacheException
- if errorprotected Cached initCachedAnnotation()
public Database getDatabase()
public java.lang.Class<R> getRowClass()
public java.lang.String getTableName()
public void setTableName(java.lang.String tableName)
NameTranslator
if specified. Use this to override the default.tableName
- table name (without schema prefix)public java.lang.String getQualifiedTableName()
public java.util.List<? extends NameTranslator> getNameTranslators()
translateName(String)
.public java.lang.String translateName(java.lang.String javaName)
NameTranslator.translate(String, Class)
for all name translators in
the order that they are defined.javaName
- class or field nameRow.nameTranslators()
,
Database.addNameTranslatorClass(Class)
public RowTranslator<R> getRowTranslator()
public java.util.List<java.lang.reflect.Field> getLazySelectCascadeFields()
SelectCascade.lazy()
true.public R newRow() throws SormulaException
SormulaException
- if errorpublic void putTypeTranslator(java.lang.Class<?> typeClass, TypeTranslator<?> typeTranslator)
Database
for all operations on this table. See
Database.putTypeTranslator(Class, TypeTranslator)
for an explanation
of translators.putTypeTranslator
in interface TypeTranslatorMap
typeClass
- class that translator operates upontypeTranslator
- to use for typeClasspublic void putTypeTranslator(java.lang.String typeClassName, TypeTranslator<?> typeTranslator)
putTypeTranslator(Class, TypeTranslator)
but uses class name. Useful for adding
primitive types like "int", "boolean", "float", etc.putTypeTranslator
in interface TypeTranslatorMap
typeClassName
- class name that translator operates upontypeTranslator
- translator to use for typeClasspublic TypeTranslator<?> getTypeTranslator(java.lang.Class<?> typeClass)
Database.getTypeTranslator(Class)
. See
Database.getTypeTranslator(Class)
for more details.getTypeTranslator
in interface TypeTranslatorMap
typeClass
- class that translator operates uponpublic java.util.List<R> selectAll() throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); List<Order> orders = table.selectAll();
SormulaException
- if errorpublic R select(java.lang.Object... primaryKeys) throws SormulaException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); int orderNumber = 123456; // primary key Order order = table.select(orderNumber);
primaryKeys
- primary key values to use for select (must be in same order as
primary key columns appear with row class)SormulaException
- if errorpublic R selectWhere(java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
whereConditionName
- name of where condition to use; empty string to select arbitrary row in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic java.util.List<R> selectAllWhere(java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
whereConditionName
- name of where condition to use; empty string to select all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic java.util.List<R> selectAllWhereOrdered(java.lang.String whereConditionName, java.lang.String orderByName, java.lang.Object... parameters) throws SormulaException
whereConditionName
- name of where condition to use; empty string to select all rows in tableorderByName
- name of order phrase to use as defined in OrderBy.name()
parameters
- parameter values for where conditionSormulaException
- if errorpublic java.util.List<R> selectAllCustom(java.lang.String customSql, java.lang.Object... parameters) throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); List<Order> orders = table.selectAllCustom("where orderdate >= '2011-01-01'");
customSql
- custom sql to be appended to base sql (for example, "where somecolumn=?")parameters
- parameter value to be set in customSqlSormulaException
- if errorpublic R selectCustom(java.lang.String customSql, java.lang.Object... parameters) throws SormulaException
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); Order order = table.selectCustom("where orderdate >= '2011-01-01'");
customSql
- custom sql to be appended to base sql (for example, "where somecolumn=?")parameters
- parameter value to be set in customSqlSormulaException
- if errorpublic int selectCount() throws SormulaException
This method will throw an exception if database returns a long for the count instead of an int. Use <Long>selectCount("*") if long result is expected.
SormulaException
- if errorpublic int selectCount(java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
This method will throw an exception if database returns a long for the count instead of an int. Use <Long>selectCount("*", whereConditionName, parameters) if long result is expected.
Example:
Database database = ... Table<Order> table = database.getTable(Order.class); // quantityExceeds is the name of a Where annotation on Order that filters quantity >= ? int bigOrderCount = table.selectCount("quantityExceeds", 100);
whereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic <T> T selectCount(java.lang.String expression) throws SormulaException
The data type returned from database is the same type as the expression. For example, if expression is a column of type integer, then the returned type is Integer. If expression is "*", then return types are database dependent.
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnSormulaException
- if errorpublic <T> T selectCount(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
The data type returned from database is the same type as the expression. For example, if expression is a column of type integer, then the returned type is Integer. If expression is "*", then return types are database dependent.
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic <T> T selectMin(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnSormulaException
- if errorpublic <T> T selectMin(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic <T> T selectMax(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnSormulaException
- if errorpublic <T> T selectMax(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic <T> T selectAvg(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnSormulaException
- if errorpublic <T> T selectAvg(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic <T> T selectSum(java.lang.String expression) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnSormulaException
- if errorpublic <T> T selectSum(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws SormulaException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnwhereConditionName
- name of where condition to use; empty string to count all rows in tableparameters
- parameter values for where conditionSormulaException
- if errorpublic int insert(R row) throws SormulaException
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); Student s = new Student(); s.setId(1234); s.setFirstName("Jeff"); s.setLastName("Miller"); s.setGraduationDate(new Date(System.currentTimeMillis())); table.insert(s);
row
- row to insertSormulaException
- if errorpublic int insertBatch(R row) throws SormulaException
This method typically is only useful if inserted row has cascaded child rows that
should be inserted in batch mode. If row has no cascades, then this method is no
more useful than insert(Object)
.
row
- row to insertSormulaException
- if errorpublic int insertAll(java.util.Collection<R> rows) throws SormulaException
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); ArrayList<Student> list = new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); table.insertAll(list);
rows
- rows to insertSormulaException
- if errorpublic int insertAllBatch(java.util.Collection<R> rows) throws SormulaException
ModifyOperation.setBatch(boolean)
.rows
- rows to insertSormulaException
- if errorpublic int update(R row) throws SormulaException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); Student s = table.select(id); s.setGraduationDate(...); table.update(s);
row
- row to updateSormulaException
- if errorpublic int updateBatch(R row) throws SormulaException
This method typically is only useful if updated row has cascaded child rows that
should be updated in batch mode. If row has no cascades, then this method is no
more useful than update(Object)
.
row
- row to updateSormulaException
- if errorpublic int updateAll(java.util.Collection<R> rows) throws SormulaException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); List<Student> list = table.selectAll(); for (Student s: list) s.setGraduationDate(...); table.updateAll(list);
rows
- rows to updateSormulaException
- if errorpublic int updateAllBatch(java.util.Collection<R> rows) throws SormulaException
The primary key is defined by Column.primaryKey()
, Column.identity()
, or
Row.primaryKeyFields()
. See limitations about batch updates
in ModifyOperation.setBatch(boolean)
.
rows
- rows to updateSormulaException
- if errorpublic int delete(java.lang.Object... parameters) throws SormulaException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); table.delete(1234); // deletes student with id of 1234
parameters
- where condition values to use for delete (must be in same order as
primary key fields appear with row class)SormulaException
- if errorpublic int delete(R row) throws SormulaException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); // delete student with id of 1234 Student s = table.select(1234); table.delete(s);
row
- get primary key values from this rowSormulaException
- if errorpublic int deleteBatch(R row) throws SormulaException
The primary key is defined by Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
This method typically is only useful if deleted row has cascaded child rows that
should be deleted in batch mode. If row has no cascades, then this method is no
more useful than delete(Object)
.
row
- get primary key values from this rowSormulaException
- if errorpublic int deleteAll(java.util.Collection<R> rows) throws SormulaException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); List<Student> list = getSomeStudents(); table.deleteAll(list);
rows
- get primary key values from each row in this collectionSormulaException
- if errorpublic int deleteAllBatch(java.util.Collection<R> rows) throws SormulaException
The primary key is defined by Column.primaryKey()
, Column.identity()
, or
Row.primaryKeyFields()
. See limitations about batch deletes
in ModifyOperation.setBatch(boolean)
.
rows
- get primary key values from each row in this collectionSormulaException
- if errorpublic int deleteAll() throws SormulaException
Example:
Database database = ... Table<Student> table = database.getTable(Student.class); table.deleteAll();
SormulaException
- if errorpublic int save(R row) throws SormulaException
SaveOperation
to update an existing row or insert row if it
is not already in database.row
- row to saveSormulaException
- if errorpublic int saveBatch(R row) throws SormulaException
This method typically is only useful if saved row has cascaded child rows that
should be saved in batch mode. If row has no cascades, then this method is no
more useful than save(Object)
.
row
- row to saveSormulaException
- if errorpublic int saveAll(java.util.Collection<R> rows) throws SormulaException
SaveOperation
to update an existing rows or insert rows if they
are not already in database.rows
- collection of new and/or existing rows to save (may be mixture of new and existing)SormulaException
- if errorpublic int saveAllBatch(java.util.Collection<R> rows) throws SormulaException
ModifyOperation.setBatch(boolean)
.rows
- collection of new and/or existing rows to save (may be mixture of new and existing)SormulaException
- if errorpublic void begin(Transaction transaction)
TransactionListener.begin(Transaction)
implementation. Normally other
classes do not invoke this method. Subclasses may override if needed.begin
in interface TransactionListener
transaction
- database transaction that invoked this methodpublic void commit(Transaction transaction)
TransactionListener.commit(Transaction)
implementation. Normally other
classes do not invoke this method. Subclasses may override if needed.commit
in interface TransactionListener
transaction
- database transaction that invoked this methodpublic void rollback(Transaction transaction)
TransactionListener.rollback(Transaction)
implementation. Normally other
classes do not invoke this method. Subclasses may override if needed.rollback
in interface TransactionListener
transaction
- database transaction that invoked this method