You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

What is Enhancement Anyway?

The JPA spec requires some type of monitoring of Entity objects, but the spec does not define how to implement this monitoring. Some JPA providers auto-generate new subclasses or proxy objects that front the user's Entity objects at runtime, while others use byte-code weaving technologies to enhance the actual Entity class objects at build time. OpenJPA supports both methods, but strongly suggests only using the build time enhancement.

Build Time Enhancement

Build time enhancement is the recommended method to use with OpenJPA, as it it the fastest and most reliable method. Please follow the links below based on your development environment:

Runtime Enhancement

When running in a Java SE environment or in a non-Java EE 5 compliant container, OpenJPA defaults to using runtime subclassing enhancement. The subclassing enhancement support was added originally as a convenience to new developers to reduce the amount of work to get a 'HelloWorld-ish' OpenJPA application working out of the box. It was never meant to run in production. So you're probably thinking that this sounds great! OpenJPA handles enhancement automatically for me and I can stop reading this post. Wrong! Subclassing has two major drawbacks. First off, it isn't nearly as fast as byte-code enhancement and the second drawback is that there are some documented functional problems when using the subclassing support. The moral of the story is, don't use this method of enhancement.

If you really want to use runtime enhancement, the following Runtime Enhancement page describes how to use the -javaagent setting in ANT and Eclipse projects.

Additional information regarding the subclassing enhancement can be found in the OpenJPA docs.

Dynamic Enhancement

OPENJPA-952 added the capability to have OpenJPA attempt to dynamically load the javaagent enhancer. If you see the following message, OpenJPA loaded the enhancer dynamically.

[java] 1453 jpa_app INFO [main] openjpa.Runtime - OpenJPA dynamically loaded the class enhancer. Any classes that were not enhanced at build time will be enhanced when the are loaded by the JVM.

This method of enhancement is intended for first time users or developers as it has a number of caveats.

  • It only works on Sun 1.6SDK, not the JRE and IBM JDK (SR8+).
  • If any unenhanced Entities are loaded by the JVM before an EntityManagerFactory is created, this method of enhancement will not work. If this condition is encountered, you will see the following warning:

    [java] 1047 jpa_app WARN [main] openjpa.Enhance - Unenhanced classes were detected even though the enhancer has ran. Ensure that the EntityManagerFactory is created prior to creating any Entities.

If your application uses some other method of enhancement, this support can be disabled by setting the following property in your persistence.xml.

<property name="openjpa.DynamicEnhancementAgent" value="false"/>

Author Attribution

The content for this page and sub-pages was adapted from content created by OpenJPA contributor Rick Curtis from the following WebSphere and Java Persistence blog entries:
http://webspherepersistence.blogspot.com/2009/02/openjpa-enhancement.html
http://webspherepersistence.blogspot.com/2009/04/openjpa-enhancement-eclipse-builder.html

  • No labels