Tuesday, April 28, 2009

Custom ORM with OpenJPA


While JPA provides a very rich set of mapping constructs, it doesn't offer much in terms of customization in situations where a domain model cannot be directly or easily mapped to the database using those constructs. This is more typically an issue when an existing application is being converted from some persistence mechanism such as a JDBC-based DAO to JPA and its domain classes cannot suffer more than minimal modifications.

For example, let's say there is an application XYZ which has a domain class named User. The User class contains an email address field which is of type string. Application XYZ expects email addresses in the form user@domain. However, the database table backing the User class stores the components of an email address in separate columns, EMAIL_USER and EMAIL_DOMAIN. The persistence mechanism used in XYZ (for example, JDBC) performs the necessary transforms in a DAO layer to compose the database columns to a single string value of the expected format and vice versa.

Converting this application to JPA would typically involve mapping the individual persistent fields for user and domain to the individual database columns and then wrappering them with the logic formerly provided by the DAO to do the transforms. While this is fairly straight forward, it requires modifications to the domain object and couples the transform logic to the entity. These types of changes can be invasive and undesirable in an existing application.

OpenJPA has an elegant solution for handling these types of mappings: mapping strategies. Custom mapping strategies can be applied to persistent fields using OpenJPA’s @org.apache.openjpa.persistence.jdbc.Strategy annotation. Strategies are most simply created by subclassing the org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler class. Although, for complex mappings you may decide to provide a full implementation class by implementing the org.apache.openjpa.jdbc.meta.ValueHandler interface. To implement a basic strategy you simply need to provide column mapping information and the methods to transform to and from a given type. The name of the custom strategy class is specified on the @Strategy annotation. At runtime, OpenJPA applies this strategy to the persistent field or property. This decouples the mapping and data transformation logic from the domain class. Below is a simple strategy for the multi-column email address mapping. It maps a single persistent field to two database columns and provides the transforms to get the data in the proper format to/from the domain model and database table.

EmailAddressStrategy.java

package strategies;

import org.apache.openjpa.jdbc.kernel.JDBCStore;
import org.apache.openjpa.jdbc.meta.ValueMapping;
import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ColumnIO;
import org.apache.openjpa.meta.JavaTypes;

public class EmailAddressStrategy extends AbstractValueHandler {

// Create the custom column mappings for this strategy
public Column[] map(ValueMapping vm, String name, ColumnIO io,
boolean adapt) {

Column user = new Column();
user.setName("EMAIL_USER");
user.setJavaType(JavaTypes.STRING);

Column domain = new Column();
domain.setName("EMAIL_DOMAIN");
domain.setJavaType(JavaTypes.STRING);
return new Column[]{ user, domain };
}

// Transform the email address value to its individual datastore column values.
public Object toDataStoreValue(ValueMapping vm, Object val,
JDBCStore store) {
if (val == null)
return null;

// Split the user and domain components into distinct values
if (val instanceof String) {
String strVal = (String)val;
String user = strVal;
String domain = null;
int atIndex = user.indexOf('@');
if (atIndex != -1) {
user = strVal.substring(0, atIndex);
domain = strVal.substring(atIndex+1);
}
return new Object[] { user, domain };
}
return val.toString();
}

// Transform the separate datastore values to an email address.
public Object toObjectValue(ValueMapping vm, Object val) {
if (val == null)
return null;

if (val instanceof Object[]) {
String user = null;
String domain = null;
Object[] objArray = (Object[])val;
if (objArray.length > 0)
user = objArray[0].toString();
if (objArray.length > 1)
domain = objArray[1].toString();
return user + "@" + domain;
}
return val.toString();
}
}

User.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.apache.openjpa.persistence.jdbc.Strategy;

@Entity
public class User {

@Id
@GeneratedValue
private int id;

// Use email address strategy on this persistent field
@Strategy("strategies.EmailAddressStrategy")
private String emailAddress;

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public String getEmailAddress() {
return emailAddress;
}
}

In short, if you find that JPA's mapping constructs are not quite adequate for a particular mapping, creating your own mapping strategy may be just the ticket.

-Jeremy

Sunday, April 19, 2009

What's in a word?

I wanted to understand what issues are important to the growing base of OpenJPA users. So I attempted a small experiment as follows:

  • Extracted the headers from the users' postings in OpenJPA Users' Mailing List.
  • The extraction process was a breeze with the help of HTML Parser. The program found a list of 1059 independent poster headers.
  • Fed the headers to Wordle to create a Word Cloud.

This is the Word Cloud after the first attempt

 

Wordle: OpenJPASupportForum

The original Word Cloud is here 

Of course, "problem" is often what makes a user to post in the forum. To focus further and because did not like "problem" being so prominent in the Word Cloud, removed the following words for the input list of headers: "OpenJPA", "entity", "mapping", "error", "using", "Re:"

Wordle: OpenJPA Word Cloud Iteration 2

The original Word Cloud is here

Tuesday, April 14, 2009

OpenJPA Enhancement Eclipse Builder

As I wrote about in a previous blog post, enhancement is HIGHLY recommended when developing an application that uses OpenJPA. The OpenJPA community has been vigilant about trying to cover every enhancement scenario, but after my last posting a reader brought up new somewhat uncovered situation. I say somewhat, because I'm sure the user could have enhanced using one of the existing methods, but it would not have been pretty. My understanding is that they are using Eclipse WTP to write and deploy their application, and orm.xml to map their Entities. We have the first two variants covered with an Eclipse plugin, but the third is something that we have not addressed. I did some research and found that creating a custom Ant builder may be the perfect fit. This builder will be executed by Eclipse after compilation has completed. Below I've documented the steps required to create an Ant builder that will enhance your Entities after each compile.

Please contact me with any questions/comments.

-Rick

For steps documented below, I have the following directory structure. Note, these steps must be followed for each project that has Entities that need to be enhanced.
/builder_project
enhance.xml <- the OpenJPA builder.... download here.
/bin <- Compile directory
/src <- Source directory
/jpa_lib <- OpenJPA binary and all jars from the lib dir of the binary download
commons-collections-3.2.jar
commons-lang-2.1.jar
commons-pool-1.3.jar
derby-10.2.2.0.jar
geronimo-jpa_2.0_spec-1.0-EA-SNAPSHOT.jar
geronimo-jta_1.1_spec-1.1.1.jar
openjpa-2.0.0-SNAPSHOT.jar
serp-1.13.1.jar
/lib <- other libs

  1. After you add the enhance.xml file to your file system, make sure to refresh your Eclipse workspace so it knows about the newly added file. Make sure that the enhance.xml file is listed in the Navigator view.
  2. Right click on the Eclipse project that you want to enhance and click on properties.
  3. Click on the builders filter, and create a new Ant builder.
  4. Name your builder, then click on "Browse Workspace" in the buildfile box. If you downloaded the enhance.xml file and refreshed your workspace, it should be listed there. If not, go back to step 1 and make sure that Eclipse detects your enhance.xml file.
  5. In the "Base Directory" box, click on the variables button and select build_project. This should refer to the root of your project. In the directory structure above, it refers to "builder_project".
  6. In the "Arguments" box you need to add the following properties -Dopenjpa.libs and -Dbuild.dir. -Dopenjpa.libs is the path to the OpenJPA libs, relative to the root of the project. -Dbuild.dir is the path to the build directory, relative to the root of the project. In the directory structure above, openjpa.libs should be set to jpa_lib and build.dir should be set to bin.
  7. Click on the "Targets" tab along the top.
  8. You need to set the enhance target to run as a part of "Manual Build" and "Auto Build".

Screenshots:





Files:
[1]http://filebin.ca/zbfbg/enhance.xml -- enhance.xml
[2]http://filebin.ca/aayeg/navigator.png -- Navigator pane
[3]http://filebin.ca/cznvmv/main.png -- Edit builder configuration - main
[4]http://filebin.ca/fqnadv/targets.png -- Edit builder configuration - targets