Tuesday, December 9, 2008

Toplink with OpenEJB 3.1

This is basically a follow up on my previous post, which deals with using EclipseLink or Toplink as your persistence provider.

With OpenEJB 3.0 you had to use a few workarounds described in my post entitled Toplink as your OpenEJB Persistence Provider.

All the steps are basically the same, ie. ensuring you have the necessary libraries on the classpath, configuring persistence.xml, etc.

The difference is in initializing your InitialContext. Previously you had to do the following:
   Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

properties.put("openejbDatasource", "new://Resource?type=DataSource");
properties.put("openejbDatasource.JdbcDriver", "org.postgresql.Driver");
properties.put("openejbDatasource.JdbcUrl", "jdbc:postgresql://localhost:5432/vds_test");
properties.put("openejbDatasource.UserName", "vds");
properties.put("openejbDatasource.Password", "vds");

System.getProperties().setProperty("toplink.target-server", "org.apache.openejb.toplink.openejb.OpenEJBTransactionController");
System.getProperties().setProperty("toplink.ddl-generation", "drop-and-create-tables");
System.getProperties().setProperty("toplink.logging.level", "INFO");
System.getProperties().setProperty("toplink.create-ddl-jdbc-file-name", "create.sql");
System.getProperties().setProperty("toplink.ddl-generation.output-mode", "both");

InitialContext initialContext = new InitialContext(properties);

Now all you need to do is this:
   Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

properties.put("openejbDatasource", "new://Resource?type=DataSource");
properties.put("openejbDatasource.JdbcDriver", "org.postgresql.Driver");
properties.put("openejbDatasource.JdbcUrl", "jdbc:postgresql://localhost:5432/vds_test");
properties.put("openejbDatasource.UserName", "vds");
properties.put("openejbDatasource.Password", "vds");

System.getProperties().setProperty("toplink.ddl-generation", "drop-and-create-tables");
System.getProperties().setProperty("toplink.logging.level", "INFO");
System.getProperties().setProperty("toplink.create-ddl-jdbc-file-name", "create.sql");
System.getProperties().setProperty("toplink.ddl-generation.output-mode", "both");

InitialContext initialContext = new InitialContext(properties);

It basically comes down to you not having to add the extra target-server property and thus no need to create the initialization class (in my example "org.apache.openejb.toplink.openejb.OpenEJBTransactionController") because OpenEJB 3.1 automatically detects and configures Toplink (or derived) providers.

Thank the OpenEJB developers for their extraordinary effort.

1 comment:

Florian Probst said...

Hi Quintin,
your post helped me a lot. I would like to create JUnit tests using OpenEJB. As you did, I used at first OpenJPA but now I would like to use toplink because our application will run on Glassfish (v2.1).
That's where the problem begins. I tried many versions of toplink, including these shiped with glassfish, but with every lib the persitence unit deploy fails with the following execption:
Exception [TOPLINK-60] (Oracle TopLink Essentials - 2.1 (Build b52-fcs (09/24/2008))): oracle.toplink.essentials.exceptions.DescriptorException
Exception Description: The method [_toplink_settDefectVector_vh] or [_toplink_gettDefectVector_vh] is not defined in the object [com.desoft.atg.enterprise.entity.TDefect2defectVector].
Internal Exception: java.lang.NoSuchMethodException: com.desoft.atg.enterprise.entity.TDefect2defectVector._toplink_gettDefectVector_vh()
Can you tell me which version of toplink you are unsing and what jar files you had on your classpath? I read that some xmlparser jars should be first on classpath, but this changes nothing. Always the same exception and this frustrating. Everything worked so fine with OpenJPA. Never thought having such huge problems when using toplink.
It would be great if you contact me on this issue!