Scritto da
  • email
  • twitter
  • linkedin
  • linkedin

OpenJPA is an object-relational mapping (ORM) solution, open source (distributed under the Apache 2.0 Licence) and extremely extensible.

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() {
              super();
              myPropPlugin = addString("openjpa.MyProp");
              }

              @Override
              public String getMyProp() {
              return myPropPlugin.get();
              }
              }

... And the broker factory.

       public class MyBrokerFactory extends JDBCBrokerFactory {

              public MyBrokerFactory(MyConf conf) {
              super(conf);
              }

              public static JDBCBrokerFactory newInstance(ConfigurationProvider cp) {
              MyConf conf = new MyConfImpl();
              cp.setInto(conf);
              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.

2 VOTIAverageAverage
Ti è stato utile questo articolo?
Dal Blog di Tirasa
Quello che facciamo, studiamo e pensiamo tutti i giorni lo condividiamo qui.
Vai al blog >