|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.arsdigita.persistence.Session
All persistence operations take place within the context of a session.
The operational persistence methods operate on the object types and
associations defined in the Persistence Definition Langauge (PDL) files.
The Session object has the operational methods for creating and
retrieving data objects. The APIs that operate on the PDL-defined
metadata are in the com.arsdigita.persistence.metadata
package.
The Session object can be retrieved from the static
SessionManager.getSession()
method.
SessionManager
Method Summary | |
DataObject |
create(ObjectType type)
Creates and returns a DataObject of the given type. |
DataObject |
create(OID oid)
Creates a new DataObject with the type of the given oid and initializes the key properties to the values specified in the oid. |
DataObject |
create(String typeName)
Creates and returns an empty DataObject of the given type. |
boolean |
delete(OID oid)
Deletes the persistent object of the given type with the given oid. |
void |
flushAll()
Force all outstanding changes to be flushed to the database. |
Connection |
getConnection()
Returns the JDBC connection associated with this session. |
int |
getDatabase()
|
MetadataRoot |
getMetadataRoot()
|
TransactionContext |
getTransactionContext()
Retrieves the TransactionContext
associated with this Session. |
DataCollection |
retrieve(ObjectType type)
Retrieves a collection of objects of the specified objectType. |
DataObject |
retrieve(OID oid)
Retrieves the DataObject specified by oid. |
DataCollection |
retrieve(String typeName)
Retrieves a collection of objects of the specified objectType. |
DataOperation |
retrieveDataOperation(String name)
Retrieves a DML data operation based on the named query. |
DataQuery |
retrieveQuery(String name)
Retrieves a persistent query object based on the named query. |
void |
startProfiling()
|
void |
stopProfiling()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public MetadataRoot getMetadataRoot()
public void startProfiling()
public void stopProfiling()
public int getDatabase()
public TransactionContext getTransactionContext()
TransactionContext
associated with this Session. Every Session has exactly one
TransactionContext object. The transaction context can be
obtained as in this example:
Session ssn = SessionManager.getSession(); TransactionContext txn = ssn.getTransactionContext();The TransactionContext can be used to:
txn.beginTxn();
txn.commitTxn();
txn.abortTxn();
if (txn.inTxn()) { System.out.println("Currently in a transaction."); }
if (txn.getTransactionIsolation() == java.sql.Connection.TRANSACTION_NONE) { System.err.println("Transaction isolation level is too low."); }
txn.setTransactionIsolation (java.sql.Connection.TRANSACTION_READ_UNCOMMITTED);
SessionManager
,
Connection
public Connection getConnection()
public DataObject create(ObjectType type)
DataObject.save()
is called.
Because of the initial null values, this method should only be
used for creating new objects. The method retrieve(OID)
is suitable for creating objects that will then be populated with
information from the database (e.g. objects that are being retrieved
rather than created as new).
type
- The type of the object to be created.
create(String)
public DataObject create(String typeName)
DataObject.set(String,Object)
.
Once this is done the object may be persisted using
DataObject.save()
.
An example:
Session ssn = SessionManager.getSession(); DataObject employee = ssn.create("com.dotcom.Employee"); employee.set("name", "John Doughnut"); employee.set("id", new BigInteger(12345)); employee.set("title", "Developer"); employee.save();
typeName
- The qualified name of the type of object to be
created.
SessionManager
public DataObject create(OID oid)
oid
- The OID that specifies the type of and key properties for
the resulting DataObject.public DataObject retrieve(OID oid)
oid
- The id of the object to be retrieved.
public boolean delete(OID oid)
oid
- The id of the object to be deleted.
public DataCollection retrieve(ObjectType type)
retrieveAll
event defined
in the PDL and then returns a DataCollection. This data collection
can be filtered and iterated over to retrieve data for the object.
type
- The type of the persistent collection.
retrieve(String)
public DataCollection retrieve(String typeName)
Retrieves a collection of objects of the specified objectType.
This method executes the retrieveAll
event defined
in the PDL and then returns a DataCollection. This data collection
can be filtered and iterated over to retrieve data for the object.
retrieveAll
event can be defined as in this
example:
retrieveAll { do { select * from users } map { firstName=users.first_name; lastName=users.last_name; } }From Java, you can retrieve all of the users as a DataCollection, and add filters.
DataCollection allUsers = session.retrieve("users"); allUsers.addEqualsFilter("firstName", "Smith") while (allUsers.next()) { System.out.println(allUsers.get("firstName") + allUsers.get("lastName") + allUsers.get("groupName")); }It is also possible to instantiate a data object from a DataCollection, using
DataCollection.getDataObject()
.
typeName
- The qualified name of the type of the object to be
created.
retrieveAll
event..retrieve(ObjectType)
public DataQuery retrieveQuery(String name)
Retrieves a persistent query object based on the named query. The query must be defined with the specified name in the the PDL.
DataQuery objects can be used to access fields from several data objects (representing columns in separate database tables) in a lightweight fashion. The example belows show you can use a DataQuery to access information about users and groups.
query UsersGroups { do { select * from users, groups, membership where users.user_id = membership.member_id and membership.group_id = groups.group_id } map { firstName=users.first_name; lastName=users.last_name; groupName=groups.group_name; }You can use this query and filter it further. Let's say I wanted to get all users whose first name is "Smith":
DataQuery query = session.retrieveQuery("UsersGroups"); query.addEqualsFilter("firstName", "Smith") while (query.next()) { System.out.println(query.get("firstName") + query.get("lastName") + query.get("groupName")); }The filter will add the necessary "where" clause to the SQL. The DataQuery can then be iterated over to display the appropriate data.
name
- The name of the query.
public DataOperation retrieveDataOperation(String name)
Retrieves a DML data operation based on the named query. A DataOperation is used to perform an operation on the data, such as a delete or an update. The example belows shows how it can be used to delete a set of categories.
data operation deleteCategories { delete from cat_categories where enabled_p = 0 }
The data operation defined in the SQL is accessed with the
DataOperation
object.
Sessions session = SessionManager.getSession(); DataOperation dop = session.retreiveDataOperation("deleteCategories"); dop.execute();
name
- The name of the data operation defined in the PDL.
public void flushAll()
FlushException
- when all changes can not be flushed
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |