|
Persistence Component |
The persistence access component, of Arch4J,
is a abstraction layer above persistence layers such as JDO,TopLink,Castor. This layer is more of
a future proofing and abstraction mechanism. So now if the persistence layer is changed
your code is shielded from it. All that needs to be created/used is the corresponding
Persistence Manager implementation.
This layer supports the concept of Transactions and Object Queries.
|
Get Started |
If you are installing as part of the full Arch4J distribution, you need to put the arch4j.jar file into your classpath.
If you are only interested in the data access component, you need the base.jar and arch4j_persistence.jar in your classpath.
You will also need any third party JDBC driver in your classpath.
|
Overview |
The persistence model is designed in a similar way to the other arch4j modules, there is a
PersistenceProvider factory from which the PersistenceManager implementation is obtained.
|
How To |
In order to use the Persistence Manager, you will need to do four things:
- Configure an implementation specific properties file.
- Get a reference to a PersistenceManager using the PersistenceProvider class.
- Use the PersistenceManager to create an ObjectQuery instance and bind values to it.
- Use the PersistenceManager to execute the ObjectQuery, and process the results.
Castor Property File Configuration
|
The property file name is persistence.properties .
This is where you define the mapping, database and Transaction Manager that Castor will use.
Property Name |
Property Value |
Required |
database.name |
<logical_database_name> |
Yes |
database.filename |
<Castor mapping file> |
Yes |
<database.transactionManager> |
<JNDI name of the transaction manager.> |
No |
|
Accessing the PersistenceManager
|
To get a reference to the PersistenceManager, you need to use the PersistenceProvider class. This class uses the
singleton pattern. To get the instance of the manager, use the getManager() method.
Examples
Getting instance:
PersistenceManager manager = PersistenceProvider.getManager();
|
|
Create an ObjectQuery
|
An ObjectQuery is used to query the PersistenceManager to obtain data insantiated as Java objects.
Query values are bound in the order they are specificed in the query.
Examples
PersistenceManager manager = PersistenceProvider.getManager();
ObjectQuery query = manager.getQuery( "SELECT m FROM org.mypackage.MyClass m WHERE MyClassAttribute = $1" );
query.bind( "first" );
|
Execute the ObjectQuery and process the results
|
Once you have a reference to the ObjectQuery, you can execute it and process
any results.
Examples
Collection results = manager.getObjects( query);
Iterator results.iterator();
while( results.hasNext() ) {
MyClass instance = (MyClass)results.next();
//do stuff
}
|
|
|