G N A - J O R M





license GPL


Example of a persistent class

                public class Person implements PersistentMarker {
                    private String     name;
                    private int        age;
                    private Address    address;
                    private CompanyIfc company;

                    plain getter/setter ...

PersistentMarker is a Markerinterface with no methods. It gives the Enhancer-Tool the hint, that this class is persistent.

You can use Interfaces like CompanyIfc as links to other persistent classes or direct links like Address.

Use the enhancer tool from console or ant script for static enhancement at build-time

enhancer . . . ant

The enhancer tool will add code and variables to your classes, so gna-jorm is able to determine if an object is modified, or fully loaded, it will create a no arg-constructor if needed, and other things. Gna-jorm contains a build.xml including a enhance-task (copy it for your build-scripts). You can also start a command-line tool via the enhance.bat located in the bin-folder.

Use the ApplicationStarter for dynamic enhancement at classes load-time

The ApplicationStarter establishes a ClassLoader wich does bytecode enhancements for every loaded class that implements the PersistentMarker interface. So you dont need the static enhancement described above. Just use the class "de.ama.db.tools.ApplicationStarter" with your app as first parameter, followed by your command-line parameters. Your App is startet via the standard "static main(String[] args)" method. If you start your application usualy like this

               java -cp ... your_application_class param1 param2 ...
you would do instead
               java -cp ... de.ama.db.tools.ApplicationStarter your_application_class param1 param2 ...

Connect to the database

                DB.createDB( "localhost", "user", "admin", "password");

Before we use a database, we have to connect to it. This is done by instantiating a DB-Object. It's Constructor takes 4 parameters : host, dbuser, first schema inside DB, db-password. Then we decide to have a seperate catolog to work in, with name "miller" (this will create a new schema "miller" in the db, if necessary). Now we are ready to use our session.

Save an object

                try {

                    Person person = new Person();
                    person.setAdress(new Address("New York"));
                    person.setCompany(new Company);


                } catch (Exception e) {

First we create some objects and link them together. Than we attach the root-object (person) to the current Db.session(). The current database-session can be reached by DB.session() it will be allways open and wayting for your comands. DB.session().setObject(..) attaches the object to the session, but only if you commit the session, the root-object (person) and all it's linked objects (Address, Company) will be persisted. Complete transaction-handling will rollback everything, if something went wrong.

Load one object

                Person person = (Person)DB.session().getObject(new Query(Person.class,"name",Query.EQ,"marc"));

After this call you have your persistent object back, and ready to use, attached to the current session. The linked objects (Address, Company) will be loaded as Hollow-objects at this moment. They will be fully loaded if you use them. If there is more than one person with name "marc", an exception will be thrown.

Load objects via path query

                OidIterator itr = DB.session().getObjects(new Query(Person.class, "address->city", Query.EQ,"New York"));

Here we see our first path-query. It finds all persons with an address-property city="New York".