An annotation processor to update your persistence.xml

Discussions

News: An annotation processor to update your persistence.xml

  1. Sahoo (no last name provided, all apologies!) has written a blog called "An annotation processor to update your persistence.xml during javac" to automatically generate a persistence.xml file for entity persistence. While aimed at EJB3 users from J2SE, the facility might be useful for J2EE users, to keep updated as well.

    From the introduction:
    When Java Persistence API is used in a managed environment (like Java EE container), there is often a deployment step and a notion of deployable module (an ear/war/jar) that represents the boundary of an application. During deployment container/persistence provider can discover managed persistence classes by introspecting .class files bundled in the application. But when Java Persistence API is used in Java SE environment, there is no such predefined deployment step, nor is there any deployable module. So a persistence provider does not know what are all the classes to introspect. Hence the spec requires users to specify the list of managed persistence classes in persistence.xml. A managed persistence class is a class that is either an Entity or Embeddable or EmbeddableSuperclass.

    In this article, I shall show how to write an Annotation Processor that can be used along with J2SE's Annotation Processing Tool to automatically generate the list of managed persistence classes and update persistence.xml during compilation of entity source code.

    Threaded Messages (7)

  2. You can figure out class set in SE[ Go to top ]

    You can figure out the set of classes that belong to a particular jar the META-INF/persistence.xml file is jarred in. Most providers (JBoss, Oracle, BEA) will/do support this. How? ClassLoader.getResources("META-INF/persistence.xml"); returns a list of all persistence.xml urls. From this url you can determine the JAR file the persistence.xml file is located and scan the JAR for annotated classes and/or orm.xml files. This is not portable *in theory* between VMs, but *in practice* works just fine. BTW, persistence.xml will be required in the next version of the spec.

    But...an article on how to write an annotation processor is a nice idea and the article was good in that regard.

    Bill
  3. xdoclet?[ Go to top ]

    Sounds like xdoclet for annotations.
  4. While autodiscovery is not portable in JavaSE for theorical reasons, I really hope most vendors will support it in "practical" environments. Having to use an annotation processor would be painful from a user perspective.
  5. Having to use an annotation processor would be painful from a user perspective.

    I agree, as of now, using an annotation processor is not straight forward because:
    1) not all JDKs support this today.
    2) user has to use apt as opposed to javac to compile their program.
    3) there is no ready made Ant task to invoke apt. Having said that it should not be a difficult task to write a custom Ant task to invoke apt.

    But it is very straight forward and easy while using any Java SE 6 compatible JDK because annotation processor can be specified as a javac option. Here is a useful link I found about this:
    http://blogs.sun.com/roller/page/darcy?entry=jsr_269_in_mustang_build

    -- Sahoo
  6. Sun JDK dependence[ Go to top ]

    Nice article, however, requires com.sun.mirror.apt package so I guess it won't work under jrockit or IBM jdks.

    I'm surprised that these annotation support classes aren't under the java.lang.annotations package.

    Anybody know why they're not? Seems like they make annotation support much easier.
  7. Sun JDK dependence[ Go to top ]

    Good point. I should have warned user about this in the article. Yes the annotation processor currently uses com.sun.mirror API and hence only works on Sun JDK 1.5. But com.sun.mirror APIs are being renamed as part of JSR 269 and it will be part of Java SE 6. Which means this program can be rewritten using Java SE 6 and will be completely portable across all Java SE 6 implementations. In fact the new API javadocs are available here.

    -- Sahoo
  8. FYI

    His email is Sanjeeb dot Sahoo at Sun dot COM (I got this from his source code)

    Sahoo should be the last name and first name is Sanjeeb.