Browse DevX
Sign up for e-mail newsletters from DevX


An Intro to Java Object Persistence with JDO : Page 2

The 'impedance mismatch' between relational databases' tabular orientation and object-oriented Java's hierarchical one is a perennial problem for which the Java world has several good solution offerings. The third in a three-part series, this article examines the ways that JDO attempts to simplify the chore of connecting relational databases and the Java programming language.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Application and Runtime Environment
I will use the JDO reference implementation found at http://www.sun.com/software/communitysource/jdo/index.html and JBoss 3.2.3 as my deployment and runtime environment for the examples that follow. I will design a simple Web application that allows user accounts to be created and retrieved using a Web browser and show you how to do the same. Client requests will be passed from a browser to a Java servlet, which communicates with a user service, which communicates with JDO-based data access objects (DAOs), as Figure 1 illustrates.

Figure 1. The Four Tiers: A high-level view of the framework's architecture is shown.
The DAO pattern abstracts and encapsulates all access to the data source. The application has one DAO interface, UserDao. The implementation class, JDOUserDao, contains JDO-specific logic to handle data-management duties for a given user.

The Web Tier Configuration
Each client HTTP request is handled by a FrontController-style servlet embodied within an instance of UserInfoServlet. The UserInfoServlet instance converts each request to a business-service request and then calls the appropriate business service for processing. The UserInfoServlet is shown in Listing 1.

The Business Tier
Each client HTTP request is converted to a business-service request and passed to the appropriate business service for processing. Each business service object performs the necessary business logic and makes use of the appropriate DAO for datastore access.

The UserService class encapsulates methods for operating on UserInfo objects including storing, updating, deleting, and retrieving instances of UserInfo. The UserService class is shown in Listing 2.

JDO Metadata Files
JDO uses XML-based metadata files to specify persistence-related information including which classes should be persistent. The metadata file can contain persistence information for a single persistent class or one or more packages that have persistent classes.

A JDO persistence-metadata file can define persistence properties—for a class or an entire package—in one or more XML files. The name of the metadata file for one class is the name of the class, followed by a .jdo suffix. Therefore, the metadata file for the UserInfo class would be named UserInfo.jdo. This must be placed in the same directory as the UserInfo.class file. Metadata for an entire package must be contained in a file named package.jdo. A .jdo file for an entire package can contain information for multiple classes and multiple sub-packages.

The following example illustrates how the UserInfo class representing a given user is configured for JDO-enhancement in the metadata file, package.jdo.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN" "http://java.sun.com/dtd/jdo_1_0.dtd"> <jdo> <package name="com.jeffhanson.businesstier.model"> <class name="UserInfo" identity-type="datastore"> <field name="id" persistence-modifier="persistent"> <extension vendor-name="jdori" key="column" value="ID"/> </field> <field name="fullName" persistence-modifier="persistent"> <extension vendor-name="jdori" key="column" value="FULLNAME"/> </field> <field name="address" persistence-modifier="persistent"> <extension vendor-name="jdori" key="column" value="ADDRESS"/> </field> <field name="city" persistence-modifier="persistent"> <extension vendor-name="jdori" key="column" value="CITY"/> </field> <field name="state" persistence-modifier="persistent"> <extension vendor-name="jdori" key="column" value="STATE"/> </field> <field name="zip" persistence-modifier="persistent"> <extension vendor-name="jdori" key="column" value="ZIP"/> </field> </class> </package> </jdo>

The Data Tier
Each business-service request is passed to the appropriate business service for processing. A business service performs the necessary business logic and makes use of the appropriate DAO for datastore access. Each DAO performs the necessary interactions with JDO in order to act upon a given datastore. The UserDAO interface defines the methods each DAO must implement:

package com.jeffhanson.datatier; import com.jeffhanson.businesstier.model.UserInfo; public interface UserDAO { public UserInfo createUser(String id, String fullName, String address, String city, String state, String zip) throws DAOException; public UserInfo readUser(String id) throws DAOException; public UserInfo[] readUsersByState(String state) throws DAOException; public void updateUser(UserInfo userInfo) throws DAOException; public void deleteUser(UserInfo userInfo) throws DAOException; }

The JDOUserDAO class provides an implementation of the UserDAO interface, which will enable access to the UserInfo object's datastore.

Thanks for your registration, follow us on our social networks to keep up-to-date