R
- record typepublic class ActiveTable<R extends ActiveRecord<? super R>>
extends java.lang.Object
ActiveRecord
objects. This class is
used by ActiveRecord
to perform single record operations. This class may also be
created explicitly to invoke collection methods like, saveAll(Collection)
,
insertAll(Collection)
, updateAll(Collection)
, deleteAll(Collection)
,
and select methods.
Since 4.1, related cascades are performed in batch mode for batch operations.
Constructor and Description |
---|
ActiveTable(ActiveDatabase activeDatabase,
java.lang.Class<R> recordClass)
Constructs for a database and record type.
|
ActiveTable(java.lang.Class<R> recordClass)
Constructs for default active database and a record type.
|
Modifier and Type | Method and Description |
---|---|
int |
delete(R record)
Deletes by primary key.
|
int |
deleteAll()
Deletes all records in table.
|
int |
deleteAll(java.util.Collection<R> records)
Deletes many records by primary key.
|
int |
deleteAllBatch(java.util.Collection<R> records)
Deletes many records by primary key in batch mode.
|
int |
deleteBatch(R record)
Deletes by primary key in batch mode.
|
void |
flush()
Writes uncommitted table cache records to database and removes them from the cache.
|
ActiveDatabase |
getActiveDatabase()
Gets the active database.
|
protected ActiveTransaction |
getActiveTransaction()
Gets the active transaction if transaction is in use.
|
Cache<R> |
getCache()
Gets this table's cache.
|
java.lang.Class<R> |
getRecordClass()
Gets the record class.
|
int |
insert(R record)
Inserts one record into table.
|
int |
insertAll(java.util.Collection<R> records)
Inserts collection of records.
|
int |
insertAllBatch(java.util.Collection<R> records)
Inserts collection of records in batch mode.
|
int |
insertBatch(R record)
Inserts an a record in batch mode.
|
boolean |
isCached()
Tests if this table is cached.
|
R |
newActiveRecord()
Creates a new active record of type R using zero-arg constructor.
|
int |
save(R record)
Updates an existing record or insert record if it is not already in database.
|
int |
saveAll(java.util.Collection<R> records)
Updates an existing records or insert records if they are not already in database.
|
int |
saveAllBatch(java.util.Collection<R> records)
Updates an existing records or insert records if they are not already in database in batch mode.
|
int |
saveBatch(R record)
Updates an existing record or insert record if it is not already in database in batch mode.
|
R |
select(java.lang.Object... primaryKeys)
Selects one record in table using primary key.
|
java.util.List<R> |
selectAll()
Selects all records in table.
|
java.util.List<R> |
selectAllCustom(java.lang.String customSql,
java.lang.Object... parameters)
Select list of records using custom sql.
|
java.util.List<R> |
selectAllWhere(java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects list of records 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 records 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.
|
<T> T |
selectCount()
Selects count of records using "select count(*) from ...".
|
<T> T |
selectCount(java.lang.String expression)
Selects count of records.
|
<T> T |
selectCount(java.lang.String expression,
java.lang.String whereConditionName,
java.lang.Object... parameters)
Selects count of records.
|
R |
selectCustom(java.lang.String customSql,
java.lang.Object... parameters)
Select list of records 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 record for where condition and parameters.
|
int |
update(R record)
Updates one record in table by primary key.
|
int |
updateAll(java.util.Collection<R> records)
Updates collection of records using primary key.
|
int |
updateAllBatch(java.util.Collection<R> records)
Updates collection of records using primary key in batch mode.
|
int |
updateBatch(R record)
Updates one record in table by primary key in batch mode.
|
public ActiveTable(java.lang.Class<R> recordClass) throws ActiveException
ActiveDatabase.setDefault(ActiveDatabase)
. The default
active database must be configured prior to first database access by this table.recordClass
- Java class of recordActiveException
- if errorpublic ActiveTable(ActiveDatabase activeDatabase, java.lang.Class<R> recordClass) throws ActiveException
activeDatabase
- active database that defines data source for recordsrecordClass
- Java class of recordActiveException
- if activeDatabase is nullpublic ActiveDatabase getActiveDatabase()
public java.lang.Class<R> getRecordClass()
public R newActiveRecord() throws ActiveException
ActiveRecord.attach(ActiveDatabase)
.ActiveException
- if errorpublic R select(java.lang.Object... primaryKeys) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on selected record to
attach it to the active database of this table.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, 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 record class)ActiveException
- if errorpublic R selectWhere(java.lang.String whereConditionName, java.lang.Object... parameters) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on selected record to
attach it to the active database of this table.whereConditionName
- name of where condition to use; empty string to select arbitrary record in tableparameters
- parameter values for where conditionActiveException
- if errorpublic R selectCustom(java.lang.String customSql, java.lang.Object... parameters) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on selected record to
attach it to the active database of this table.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, 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 customSqlActiveException
- if errorpublic java.util.List<R> selectAll() throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on selected records to
attach them to the active database of this table.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, Order.class); List<Order> orders = table.selectAll();
ActiveException
- if errorpublic java.util.List<R> selectAllWhere(java.lang.String whereConditionName, java.lang.Object... parameters) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on selected records to
attach them to the active database of this table.whereConditionName
- name of where condition to use; empty string to select all records in tableparameters
- parameter values for where conditionActiveException
- if errorpublic java.util.List<R> selectAllWhereOrdered(java.lang.String whereConditionName, java.lang.String orderByName, java.lang.Object... parameters) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on selected records to
attach them to the active database of this table.whereConditionName
- name of where condition to use; empty string to select all records in tableorderByName
- name of order phrase to use as defined in OrderBy.name()
parameters
- parameter values for where conditionActiveException
- if errorpublic java.util.List<R> selectAllCustom(java.lang.String customSql, java.lang.Object... parameters) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on selected records to
attach them to the active database of this table.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Order> table = new ActiveTable<Order>(activeDatabase, 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 customSqlActiveException
- if errorpublic <T> T selectCount() throws ActiveException
The data type returned from database is the same type as the expression. The return type is database dependent since expression is "*".
T
- aggregate result typeActiveException
- if errorpublic <T> T selectCount(java.lang.String expression) throws ActiveException
The data type returned from database is the same type as the expression. For example, if expression is a column, then the returned type is the same type as column. 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 columnActiveException
- if errorpublic <T> T selectCount(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws ActiveException
The data type returned from database is the same type as the expression. For example, if expression is a column, then the returned type is the same type as column. 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 records in tableparameters
- parameter values for where conditionActiveException
- if errorpublic <T> T selectMin(java.lang.String expression) throws ActiveException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnActiveException
- if errorpublic <T> T selectMin(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws ActiveException
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 records in tableparameters
- parameter values for where conditionActiveException
- if errorpublic <T> T selectMax(java.lang.String expression) throws ActiveException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnActiveException
- if errorpublic <T> T selectMax(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws ActiveException
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 records in tableparameters
- parameter values for where conditionActiveException
- if errorpublic <T> T selectSum(java.lang.String expression) throws ActiveException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnActiveException
- if errorpublic <T> T selectSum(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters)
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 records in tableparameters
- parameter values for where conditionActiveException
- if errorpublic <T> T selectAvg(java.lang.String expression) throws ActiveException
T
- aggregate result typeexpression
- expression to use as parameter to function; typically it is the name of a columnActiveException
- if errorpublic <T> T selectAvg(java.lang.String expression, java.lang.String whereConditionName, java.lang.Object... parameters) throws ActiveException
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 records in tableparameters
- parameter values for where conditionActiveException
- if errorpublic int save(R record) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on record to
attach it to the active database of this table prior to save.
Typically ActiveRecord.save()
is used instead of this method.record
- record to saveActiveException
- if errorpublic int saveBatch(R record) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on record to
attach it to the active database of this table prior to save.
Typically ActiveRecord.save()
is used instead of this method.record
- record to saveActiveException
- if errorpublic int saveAll(java.util.Collection<R> records) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to saving them.records
- collection of new and/or existing records to save (may be mixture of new and existing)ActiveException
- if errorpublic int saveAllBatch(java.util.Collection<R> records) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to saving them.records
- collection of new and/or existing records to save (may be mixture of new and existing)ActiveException
- if errorpublic int insert(R record) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on record to
attach it to the active database of this table prior to insert.
Typically ActiveRecord.insert()
is used instead of this method.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); Student s = new Student(); s.setId(1234); s.setFirstName("Jeff"); s.setLastName("Miller"); s.setGraduationDate(new Date(System.currentTimeMillis())); table.insert(s);
record
- record to insertActiveException
- if errorpublic int insertBatch(R record) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on record to
attach it to the active database of this table prior to insert.
Typically ActiveRecord.insertBatch()
is used instead of this method.record
- record to insertActiveException
- if errorpublic int insertAll(java.util.Collection<R> records) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to insert.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); ArrayList<Student> list = new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); table.insertAll(list);
records
- records to insertActiveException
- if errorpublic int insertAllBatch(java.util.Collection<R> records) throws ActiveException
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to insert.
See limitations about batch inserts in ModifyOperation.setBatch(boolean)
.
records
- records to insertActiveException
- if errorpublic int update(R record) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on record to
attach it to the active database of this table prior to update.
Typically ActiveRecord.update()
is used instead of this method.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); Student s = table.select(id); s.setGraduationDate(...); table.update(s);
record
- record to updateActiveException
- if errorpublic int updateBatch(R record) throws ActiveException
record
- record to updateActiveException
- if errorpublic int updateAll(java.util.Collection<R> records) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to update.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); List<Student> list = table.selectAll(); for (Student s: list) s.setGraduationDate(...); table.updateAll(list);
records
- records to updateActiveException
- if errorpublic int updateAllBatch(java.util.Collection<R> records) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to update.
See limitations about batch updates in ModifyOperation.setBatch(boolean)
.
records
- records to updateActiveException
- if errorpublic int delete(R record) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on record to
attach it to the active database of this table prior to delete.
Typically ActiveRecord.delete()
is used instead of this method.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); // delete student with id of 1234 Student s = table.select(1234); table.delete(s);
record
- get primary key values from this recordActiveException
- if errorpublic int deleteBatch(R record) throws ActiveException
record
- get primary key values from this recordActiveException
- if errorpublic int deleteAll() throws ActiveException
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); table.deleteAll();
ActiveException
- if errorpublic int deleteAll(java.util.Collection<R> records) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to delete.
Example:
ActiveDatabase activeDatabase = ... ActiveTable<Student> table = new ActiveTable<Student>(activeDatabase, Student.class); List<Student> list = getSomeStudents(); table.deleteAll(list);
records
- get primary key values from each record in this collectionActiveException
- if errorpublic int deleteAllBatch(java.util.Collection<R> records) throws ActiveException
Column.primaryKey()
,
Column.identity()
, or Row.primaryKeyFields()
.
ActiveRecord.attach(ActiveDatabase)
is invoked on records to
attach them to the active database of this table prior to delete.
records
- get primary key values from each record in this collectionActiveException
- if errorpublic boolean isCached() throws ActiveException
ActiveException
- if errorpublic Cache<R> getCache() throws ActiveException
ActiveException
- if errorpublic void flush() throws ActiveException
ActiveException
- if errorprotected ActiveTransaction getActiveTransaction()