Section 14.7. Beyond the Basics


14.7. Beyond the Basics

We are going to adopt a very simple strategy for database persistence. We are going to read in the data structures at startup and maintain them as changes are made during execution. That way, any abnormal termination will leave the data in a recoverable state. The application will not require any "save" operation.

Example 14.1. Candidate DB tables for BudgetPro
 DROP DATABASE budgetPro; CREATE DATABASE budgetPro; USE DATABASE budgetPro; CREATE TABLE Account (   id INTEGER NOT NULL,   name VARCHAR(64) NOT NULL,   user_id INTEGER NOT NULL,   amount DECIMAL,   balance DECIMAL,   parent_account_id INTEGER,   PRIMARY KEY (id),   FOREIGN KEY (user_id) REFERENCES User(id) ); CREATE TABLE User (   id INTEGER NOT NULL,   name VARCHAR(64),   PRIMARY KEY (id) ); 

We will implement this in the simplest way possible, by directly embedded SQL statements in the application code. But this is far from your only choice.

It is possible to design a "persistence framework," such that all classes that inherit from a persistence base class or implement a persistence interface and follow certain naming conventions for member data can be automatically backed by persistent storage. Java's ability to introspect, that is, to look at the names and structures of a class dynamically at runtime, allow one to write such an automatic persistence framework. Several such libraries already exist, including both commercial and Open Source options. This being an Open Source book, we'll call you attention to the Open Source choices:[3]

[3] Have we tried all of these? Yeah. Sure. Why not? Of course we haven't. Don't read endorsement into this or any of our "here are some choices" lists. These are also not presented in any particular order. We tell you they exist. It is up to you to evaluate their suitability for your purposes.

Hibernate[4]

[4] http://www.hibernate.org/

Hibernate is probably the most widely known Open Source persistence framework. It is released under the LGPL license.

OJB[5]

[5] http://db.apache.org/ojb/

Billed as "Object Relational Bridge," this one is from Apache. It provides both an ODMG (a persistence API much used in the C++ world) and a JDO (Java Data ObjectsSun's object persistence API specification) API.

Castor[6]

[6] http://castor.exolab.org/

Castor does persistence to both XML and relational databases. They call their RDBMS persistence framework "JDO," but beware: It is not compatible with or identical to Sun's JDO. They say it is better.

pBeans[7]

[7] http://pbeans.sourceforge.net/

pBeans does fully automated persistence of JavaBeans. You have to follow the rules for writing beans (not EJBs, but the "classic" JavaBeans), but once done, this will automatically persist your instances to any JDBC-compatible database. No XML specification, no SQL scripting, no templates. For the easiest "just save my instance data" type of applications, this can be a good choice.[8] This product even automates the creation of tables, as we advocated above.

[8] But we are not endorsing here.

Are there others? Heck, yes. Not all of them persist to relational databases. Some persist only to XML. Some to other databases like B-trees or the Berkeley DB system. We didn't concern ourselves with those. We also left off a couple of libraries that appear not to have been developed for more than a couple of years.



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net