03 Sep 2012

OpenJPA Configuration Extension

Written by fabio

Yes, Apache OpenJPA is really extensible!

Recently, working on a library of OpenJPA's pluggable components providing support for Microsoft SQLAzure, I needed to add some new configuration parameters.

Unfortunately, all the new parameters provided into persistence.xml file will be ignored by OpenJPA: each configuration parameter must be explicitly considered by the currently used OpenJPA Configuration implementation.

To extend OpenJPA configuration you have to provide your custom implementation of OpenJPAConfiguration class. Unfortunately, this component is not explicitly pluggable but you can provide it by overriding the BrokerFactory.

Following my simple solution to add a custom configuration parameter openjpa.MyProp.

First of all the configuration interface

      public interface MyConf extends JDBCConfiguration {
              public String getMyProp();

... Then its implementation

      public class MyConfImpl extends JDBCConfigurationImpl implements MyConf {

              protected StringValue myPropPlugin;

              public MyConfImpl() {
              myPropPlugin = addString("openjpa.MyProp");

              public String getMyProp() {
              return myPropPlugin.get();

... And the broker factory.

       public class MyBrokerFactory extends JDBCBrokerFactory {

              public MyBrokerFactory(MyConf conf) {

              public static JDBCBrokerFactory newInstance(ConfigurationProvider cp) {
              MyConf conf = new MyConfImpl();
              return new MyBrokerFactory(conf);

Providing the parameter openjpa.BrokerFactory valued with MyBrokerFactory's full name, the new configuration is in place and the value of the property openjpa.MyProp is available through MyConf's getMyProp method.


« Return