Avoiding OpenJPA annotations in the code


OpenJPA, as a lot of JPA providers, supports some custom configuration through specific annotations. That’s great but it makes your code not portable.

It is often not known but OpenJPA comes with the ability to replace these annotations (at least a lot of these annotations) by a simple XML configuration.

OpenJPA doesn’t have a custom xml file. It simply enriches orm.xml file. However i think it is a good practise to separate standard orm.xml configuration from openjpa one (i personally like to add a mapping-file called openjpa.xml).

I’ll not detail the whole configuration and simply write here the main pointers to be able to start to replace the OpenJPA annotations by some XML lines.

First step: the persistence.xml

As described below this step is optional ; it simply tries to respect the responsability principle.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="user">
    ...
    <mapping-file>META-INF/orm.xml</mapping-file>
    <mapping-file>META-INF/openjpa.xml</mapping-file>
    ...
  </persistence-unit>
</persistence>

The first file can even be omitted 🙂

Create the openjpa.xml file

The important part is here: create an orm.xml file but with openjpa namespaces:

<?xml version="1.0"?>
<entity-mappings xmlns="http://www.apache.org/openjpa/ns/orm/extendable"
                 xmlns:openjpa="http://www.apache.org/openjpa/ns/orm"
                 xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 version="2.0">
  <openjpa:openjpa-version version="1.0"/>

</entity-mappings>

We can see the standard namespace redirected under “orm”, the extended one for OpenJPA as default and the “magic” one under “openjpa”.

Last step: use it!

Now you have the namespace correctly set up your IDE should be happy and able to help you (at least that’s the case for Idea).

You can start defining an entity:

<entity class="org.Person">
....
</entity>

And now the interesting part. Suppose you use EhCache to cache your entities. OpenJPA will use the datacache name as EhCache region. To configure it by entity you can use the @DataCache annotation but it makes your code importing this annotation (org.apache.openjpa…). To avoid it the new file you created can define it in XML. Simply add to your entity an openjpa:entity block with an openjpa:data-cache configuration:

<entity class="org.Person">
  <openjpa:entity discriminator-strategy="class-name">
    <openjpa:data-cache name="person" />
  </openjpa:entity>
</entity>

And that’s all! Now define in your ehcache.xml a region called person and you’ll be able to configure the cache for this entity only :).

Another good usage of such a configuration can be to define fetch group.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s