public interface ActiveObjects
| Modifier and Type | Method and Description |
|---|---|
<K> int |
count(Class<? extends net.java.ao.RawEntity<K>> type)
Counts all entities of the specified type.
|
<K> int |
count(Class<? extends net.java.ao.RawEntity<K>> type,
net.java.ao.Query query)
Counts all entities of the specified type matching the given
Query
instance. |
<K> int |
count(Class<? extends net.java.ao.RawEntity<K>> type,
String criteria,
Object... parameters)
Counts all entities of the specified type matching the given criteria
and parameters.
|
<T extends net.java.ao.RawEntity<K>,K> |
create(Class<T> type,
net.java.ao.DBParam... params)
Creates a new entity of the specified type with the optionally specified
initial parameters.
|
<T extends net.java.ao.RawEntity<K>,K> |
create(Class<T> type,
Map<String,Object> params)
Creates and INSERTs a new entity of the specified type with the given map of
parameters.
|
void |
delete(net.java.ao.RawEntity<?>... entities)
Deletes the specified entities from the database.
|
<K> int |
deleteWithSQL(Class<? extends net.java.ao.RawEntity<K>> type,
String criteria,
Object... parameters)
Deletes rows from the table corresponding to
type. |
<T> T |
executeInTransaction(com.atlassian.sal.api.transaction.TransactionCallback<T> callback)
Execute the given callback within a transaction if the host supports transactions,
otherwise executes the callback immediately.
|
<T extends net.java.ao.RawEntity<K>,K> |
find(Class<T> type)
Returns all entities of the given type.
|
<T extends net.java.ao.RawEntity<K>,K> |
find(Class<T> type,
net.java.ao.Query query)
Selects all entities matching the given type and
Query. |
<T extends net.java.ao.RawEntity<K>,K> |
find(Class<T> type,
String criteria,
Object... parameters)
Convenience method to select all entities of the given type with the
specified, parameterized criteria.
|
<T extends net.java.ao.RawEntity<K>,K> |
find(Class<T> type,
String field,
net.java.ao.Query query)
Selects all entities of the specified type which match the given
Query. |
<T extends net.java.ao.RawEntity<K>,K> |
findWithSQL(Class<T> type,
String keyField,
String sql,
Object... parameters)
Executes the specified SQL and extracts the given key field, wrapping each
row into a instance of the specified type.
|
void |
flush(net.java.ao.RawEntity<?>... entities)
Flushes the value caches of the specified entities along with all of the relevant
relations cache entries.
|
void |
flushAll()
Flushes all value caches contained within entities controlled by this
EntityManager
instance. |
<T extends net.java.ao.RawEntity<K>,K> |
get(Class<T> type,
K... keys)
Returns an array of entities of the specified type corresponding to the
varargs primary keys.
|
<T extends net.java.ao.RawEntity<K>,K> |
get(Class<T> type,
K key)
Cleverly overloaded method to return a single entity of the specified type
rather than an array in the case where only one ID is passed.
|
void |
migrate(Class<? extends net.java.ao.RawEntity<?>>... entities)
Creates the schema for the specified entities
|
void |
migrateDestructively(Class<? extends net.java.ao.RawEntity<?>>... entities)
Create the schema for the specified entities, dropping columns and tables which are no longer required
|
ActiveObjectsModuleMetaData |
moduleMetaData()
Provides information about the state of the ActiveObjects module
|
<T extends net.java.ao.RawEntity<K>,K> |
stream(Class<T> type,
net.java.ao.EntityStreamCallback<T,K> streamCallback)
Optimised read for large datasets.
|
<T extends net.java.ao.RawEntity<K>,K> |
stream(Class<T> type,
net.java.ao.Query query,
net.java.ao.EntityStreamCallback<T,K> streamCallback)
Selects all entities of the given type and feeds them to the callback, one by one.
|
void migrate(Class<? extends net.java.ao.RawEntity<?>>... entities)
void migrateDestructively(Class<? extends net.java.ao.RawEntity<?>>... entities)
void flushAll()
EntityManager
instance. This does not actually remove the entities from the instance cache maintained
within this class. Rather, it simply dumps all of the field values cached within the entities
themselves (with the exception of the primary key value). This should be used in the case
of a complex process outside AO control which may have changed values in the database. If
it is at all possible to determine precisely which rows have been changed, the
flush(net.java.ao.RawEntity...) method should be used instead.void flush(net.java.ao.RawEntity<?>... entities)
<T extends net.java.ao.RawEntity<K>,K> T[] get(Class<T> type, K... keys)
No checks are performed to ensure that the key actually exists in the database for the specified object. Thus, this method is solely a Java memory state modifying method. There is no database access involved. The upshot of this is that the method is very very fast. The flip side of course is that one could conceivably maintain entities which reference non-existent database rows.
type - The type of the entities to retrievekeys - The primary keys corresponding to the entities to retrieve. All
keys must be typed according to the generic type parameter of the entity's
RawEntity inheritance (if inheriting from Entity, this is Integer
or int). Thus, the keys array is type-checked at compile time.<T extends net.java.ao.RawEntity<K>,K> T get(Class<T> type, K key)
get method
and functions as syntactical sugar.type - The type of the entity instance to retrievekey - The primary key corresponding to the entity to be retrievedget(Class, Object...)<T extends net.java.ao.RawEntity<K>,K> T create(Class<T> type, net.java.ao.DBParam... params)
The DBParam object parameters are designed to allow the creation
of entities which have non-null fields which have no defalut or auto-generated
value. Insertion of a row without such field values would of course fail,
thus the need for db params. The db params can also be used to set
the values for any field in the row, leading to more compact code under
certain circumstances.
Unless within a transaction, this method will commit to the database immediately and exactly once per call. Thus, care should be taken in the creation of large numbers of entities. There doesn't seem to be a more efficient way to create large numbers of entities, however one should still be aware of the performance implications.
This method delegates the action INSERT action to
This is necessary because not all databases support the JDBC RETURN_GENERATED_KEYS
constant (e.g. PostgreSQL and HSQLDB). Thus, the database provider itself is
responsible for handling INSERTion and retrieval of the correct primary key
value.
type - The type of the entity to INSERTparams - An optional varargs array of initial values for the fields in the row. These
values will be passed to the database within the INSERT statement.DBParam<T extends net.java.ao.RawEntity<K>,K> T create(Class<T> type, Map<String,Object> params)
create(Class, DBParam...)
method. The idea behind having a separate convenience method taking a map is in
circumstances with large numbers of parameters or for people familiar with the
anonymous inner class constructor syntax who might be more comfortable with
creating a map than with passing a number of objects.type - The type of the entity to INSERTparams - A map of parameters to pass to the INSERTcreate(Class, DBParam...)void delete(net.java.ao.RawEntity<?>... entities)
This method does attempt to group the DELETE statements on a per-type
basis. Thus, if you pass 5 instances of EntityA and two
instances of EntityB, the following SQL prepared statements
will be invoked:
DELETE FROM entityA WHERE id IN (?,?,?,?,?);
DELETE FROM entityB WHERE id IN (?,?);
Thus, this method scales very well for large numbers of entities grouped
into types. However, the execution time increases linearly for each entity of
unique type.entities - A varargs array of entities to delete. Method returns immediately if length == 0.<K> int deleteWithSQL(Class<? extends net.java.ao.RawEntity<K>> type, String criteria, Object... parameters)
type. In contrast to delete(RawEntity[]),
this method allows you to delete rows without creating entities for them first.
Example:
manager.deleteWithSQL(Person.class, "name = ?", "Charlie")
The SQL in criteria is not parsed or modified in any way by ActiveObjects, and is simply appended
to the DELETE statement in a WHERE clause. The above example would cause an SQL statement similar to the
following to be executed:
DELETE FROM people WHERE name = 'Charlie';
If criteria is null, this method deletes all rows from the table corresponding to type.
This method does not attempt to determine the set of entities affected by the statement. As such, it is
recommended that you call flushAll() after calling this method.
type - The entity type corresponding to the table to delete fromcriteria - An optional SQL fragment specifying which rows to deleteparameters - A varargs array of parameters to be passed to the executed prepared statement. The length
of this array must match the number of parameters (denoted by the '?' char) in criteria.delete(RawEntity...),
find(Class, String, Object...),
findWithSQL(Class, String, String, Object...)<T extends net.java.ao.RawEntity<K>,K> T[] find(Class<T> type)
find(Class, net.java.ao.Query) method.type - The type of entity to retrieve<T extends net.java.ao.RawEntity<K>,K> T[] find(Class<T> type, String criteria, Object... parameters)
criteria String
specified is appended to the SQL prepared statement immediately
following the WHERE.
Example:
manager.find(Person.class, "name LIKE ? OR age > ?", "Joe", 9);
This actually delegates the call to the find(Class, net.java.ao.Query)
method, properly parameterizing the Query object.type - The type of the entities to retrievecriteria - A parameterized WHERE statement used to determine the resultsparameters - A varargs array of parameters to be passed to the executed
prepared statement. The length of this array must match the number of
parameters (denoted by the '?' char) in the criteria.<T extends net.java.ao.RawEntity<K>,K> T[] find(Class<T> type, net.java.ao.Query query)
Query. By default, the
entities will be created based on the values within the primary key field for the
specified type (this is usually the desired behavior).
Example:
manager.find(Person.class, Query.select().where("name LIKE ? OR age > ?", "Joe", 9).limit(10));
This method delegates the call to find(Class, String, net.java.ao.Query), passing the
primary key field for the given type as the String parameter.type - The type of the entities to retrievequery - The Query instance to be used to determine the results<T extends net.java.ao.RawEntity<K>,K> T[] find(Class<T> type, String field, net.java.ao.Query query)
Query. This method creates a PreparedStatement
using the Query instance specified against the table
represented by the given type. This query is then executed (with the
parameters specified in the query). The method then iterates through
the result set and extracts the specified field, mapping an entity
of the given type to each row. This array of entities is returned.type - The type of the entities to retrievefield - The field value to use in the creation of the entities. This is usually
the primary key field of the corresponding table.query - The Query instance to use in determining the results<T extends net.java.ao.RawEntity<K>,K> T[] findWithSQL(Class<T> type, String keyField, String sql, Object... parameters)
PreparedStatement with the given parameters.
Example:
manager.findWithSQL(Person.class, "personID", "SELECT personID FROM chairs WHERE position < ? LIMIT ?", 10, 5);
The SQL is not parsed or modified in any way by ActiveObjects. As such, it is
possible to execute database-specific queries using this method without realizing
it. For example, the above query will not run on MS SQL Server or Oracle, due to
the lack of a LIMIT clause in their SQL implementation. As such, be extremely
careful about what SQL is executed using this method, or else be conscious of the
fact that you may be locking yourself to a specific DBMS.type - The type of the entities to retrievekeyField - The field value to use in the creation of the entities. This is usually
the primary key field of the corresponding table.sql - The SQL statement to execute.parameters - A varargs array of parameters to be passed to the executed
prepared statement. The length of this array must match the number of
parameters (denoted by the '?' char) in the criteria.<T extends net.java.ao.RawEntity<K>,K> void stream(Class<T> type, net.java.ao.EntityStreamCallback<T,K> streamCallback)
Please see stream(Class, Query, EntityStreamCallback) for details / limitations.
type - The type of the entities to retrievestreamCallback - The receiver of the data, will be passed one entity per returned row<T extends net.java.ao.RawEntity<K>,K> void stream(Class<T> type, net.java.ao.Query query, net.java.ao.EntityStreamCallback<T,K> streamCallback)
Accessor
methods. Calling setters or savewill result in an exception. Other method calls will be ignored. The proxies do not support lazy-loading of related entities.
This call is optimised for efficient read operations on large datasets. For best memory usage, do not buffer the entities passed to the callback but process and discard them directly. Unlike regular Entities, the read only implementations do not support flushing/refreshing. The data is a snapshot view at the time of query.
type - The type of the entities to retrievequery - The Query instance to use in determining the resultsstreamCallback - The receiver of the data, will be passed one entity per returned row<K> int count(Class<? extends net.java.ao.RawEntity<K>> type)
count(Class<? extends Entity>, Query)type - The type of the entities which should be counted.<K> int count(Class<? extends net.java.ao.RawEntity<K>> type, String criteria, Object... parameters)
count(type, Query.select().where(criteria, parameters))type - The type of the entities which should be countedcriteria - A parameterized WHERE statement used to determine the result set which will be countedparameters - A varargs array of parameters to be passed to the executed
prepared statement. The length of this array must match the number of
parameters (denoted by the '?' char) in the criteria.<K> int count(Class<? extends net.java.ao.RawEntity<K>> type, net.java.ao.Query query)
Query
instance. The SQL runs as a SELECT COUNT(*) to
ensure maximum performance.type - The type of the entities which should be countedquery - The Query instance used to determine the result set which will be counted<T> T executeInTransaction(com.atlassian.sal.api.transaction.TransactionCallback<T> callback)
callback - the callback to execute within a transactionActiveObjectsModuleMetaData moduleMetaData()
Copyright © 2018 Atlassian. All rights reserved.