Using Java Data Objects
by Dion Almaer02/06/2002
In a past article on XML Databinding, I showed how you could work with Java objects and have them persist as XML files.
In this article, I'll discuss Sun's Java Data Objects (JDO) standard. JDO allows you to persist Java objects, supporting transactions and multiple users. It differs from JDBC in that you don't have to think about SQL and "all that database stuff." It differs from serialization as it allows multiple users and transactions. This standard allows Java developers to use their object model as a data model, too. There is no need to spend time going between the "data" side and the "object" side.
Various products -- including CocoBase, WebGain TOPLink, and Castor JDO -- try to do this for you. Now that there is a standard way to do this, we get the benefit of only having to learn one way to do it. This is just like when JDBC came along and allowed us to work with any database that provided a driver. Great stuff!
We will take the Address Book example from my XML data binding article and "port" it over the JDO world. The JDO implementation that I will use is OpenFusion JDO from Prism Technologies. As you'll see, there is only one part of the code that uses a PrismTech API; everything else uses the standard JDO API (with the implementation hidden behind the scenes).
|
The code for this article can be downloaded as a zip file here. |
We will walk through the following tasks:
- Data Object: Create the
Personobject that we wish to persist in the database. - Persist: Create a
PersonPersistobject that will take care of persisting, reading, and updating thePersons in the datastore. - JDOEnhancer: Create a JDO descriptor to tell the JDOEnhancer what we are doing.
- Build: Go through the steps to build and run the system.
Data Object: Create the Person Object
We'll start with the same Person object we defined in the XML article. This object follows the
standard JavaBean conventions of get and set on the attributes. Notice that although we are
persisting this class, there is nothing special about it. It doesn't have to inherit or implement
any persistent interface/base class. The requirements for a class that can be persisted are:
- Fields must be accessible to the JDO classes (
public, orset*methods). - The field's data type must be permitted in accordance with the JDO spec (see section 6.4 for detailed information).
- Certain fields can not be supported (unserializable ones like
Thread,File,Socket).
With these requirements in mind, let's look at Person.java:
public class Person {
private String name;
private String address;
private String ssn;
private String email;
private String homePhone;
private String workPhone;
// -- allows us to create a Person via the constructor
public Person(String name, String address, String ssn,
String email, String homePhone, String workPhone) {
this.name = name;
this.address = address;
this.ssn = ssn;
this.email = email;
this.homePhone = homePhone;
this.workPhone = workPhone;
}
// -- accessors
public String getName() { return name; }
public String getAddress() { return address; }
public String getSsn() { return ssn; }
public String getEmail() { return email; }
public String getHomePhone() { return homePhone; }
public String getWorkPhone() { return workPhone; }
// -- mutators
public void setName(String name) { this.name = name; }
public void setAddress(String address) {
this.address = address;
}
public void setSsn(String ssn) { this.ssn = ssn; }
public void setEmail(String email) { this.email = email; }
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
}
Persist: Create a PersonPersist Object to Manage Persistence
So we have a Person object that we want to work with. Now we need to create something that will manage the persistence. Let's walk through the code, and see how we:
- Initialize the JDO Persistence Manager.
- Persist three "people" to the database.
- Display the people from the database.
- Change the name of one of the people.
- Delete a person.
- Run through these things in the
main()method.
Step One: Initialize the JDO Persistence Manager
Here we have the beginning of the PersonPersist object. We import the standard JDO classes
and the ManagedConnectionFactory from the OpenFusion implementation. We could have
abstracted that out to a separate class, of course. The constructor sets the connection factory,
using the javax.jdo.PersistenceManagerFactoryClass property. This is like
setting the database driver property in the JDBC world.
package addressbook;
import java.util.*;
import javax.jdo.*;
import
com.prismt.j2ee.connector.jdbc.ManagedConnectionFactoryImpl;
public class PersonPersist
{
private final static int SIZE = 3;
private PersistenceManagerFactory pmf = null;
private PersistenceManager pm = null;
private Transaction transaction = null;
// Array of people to persist
private Person[] people;
// Vector of current object identifiers
private Vector id = new Vector(SIZE);
public PersonPersist() {
try {
Properties props = new Properties();
props.setProperty("javax.jdo.PersistenceManagerFactoryClass",
"com.prismt.j2ee.jdo.PersistenceManagerFactoryImpl");
pmf = JDOHelper.getPersistenceManagerFactory(props);
pmf.setConnectionFactory( createConnectionFactory() );
} catch(Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
The connection factory is created in a static method named
createConnectionFactory(). This factory needs the JDBC URL, the JDBC driver, a
username, and a password. OpenFusion JDO also comes with a DBHelper that looks for a
properties file containing the database settings to use.
public static Object createConnectionFactory() {
ManagedConnectionFactoryImpl mcfi = new
ManagedConnectionFactoryImpl();
Object connectionFactory = null;
try {
mcfi.setUserName("scott");
mcfi.setPassword("tiger");
mcfi.setConnectionURL(
"jdbc:oracle:thin:@localhost:1521:thedb");
mcfi.setDBDriver("oracle.jdbc.driver.OracleDriver");
connectionFactory = mcfi.createConnectionFactory();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
return connectionFactory;
}
Pages: 1, 2 |