JPA 2.0 Typesafe Criteria API and Annotation Processing Howto

Maia build subsystem now recognizes Java Annotation Processors. Most of JPA 2.0 implementations are expected to make use of the Pluggable Annotation Processing API to automatically generate static metamodel classes. Here is a list of steps required to generate JPA 2.0 metamodel and keep it up-to-date with Hibernate Static Metamodel Generator. For the simplicity in this sample project the processor jar file is added to the module dependencies.

  • Enable annotation processing
  • Select Obtain processors from project classpath option
  • Add a processor class name to the Processors list
  • Add a module with annotated classes to the list of modules to process
  • To output generated classes to a generated directory under module content root, set Generated Sources Directory Name to generated. Note that this directory will not be set as a source root automatically.

Now, Annotation processors will be run every time you invoke Make or Compile. Besides, there is a special menu item Process Annotations that does exactly what its name says. In this sample project I have added the generated directory to project sources to get completion in JPA 2.0 Typesafe Criteria API calls. Enjoy, Comment.



UPDATE: For all those having troubles configuring EclipseLink CanonicalModelProcessor here is the preconfigured sample project with plain JPA and Web+JPA modules.

This entry was posted in New Features and tagged , , , . Bookmark the permalink.

12 Responses to JPA 2.0 Typesafe Criteria API and Annotation Processing Howto

  1. David Vallner says:

    Will this also work with annotation processors that emit modified existing classes? (Notably Project Lombok: http://projectlombok.org/ )

  2. Gregory Shrago says:

    I’ve tested the GetterSetterExample from the site.
    The build part works fine but there is no resolve/completion for generated getters/setters as far as IDEA works with sources and not generated output.

  3. I have the same question as David Vallner, re:

    http://projectlombok.org/features/Data.html

    I’d like to have IDEA recognize the generated classes, and no longer give me red squiggles for calling generated methods (getters/setters in this case). Navigation to the generate class would be a bonus.

  4. Peter says:

    I really appreciate that feature. However unless the generated classes are recognized by the IDEA and the generated sources are indexed and included into the type hierarchy it’s pretty useless :-(

    Guess this will come in a later milestone of Maia :-)

  5. If your are unhappy with the JPA2 APT metamodel, you should given Querydsl a try. It provides a more compact, readable and typesafe metamodel :

    http://source.mysema.com/display/querydsl/Querydsl

  6. Gregory Shrago says:

    The generated sources are indexed if the ‘generated’ directory is marked as a source folder in the corresponding module settings. Anyway Peter is right that currently IDEA does not do anything with the generated classes.

  7. Peter says:

    Does anybody know how to debug annotation processors in IDEA?

  8. Noah says:

    Hi,

    I tried setting things up for Eclipselink but the Build -> Process Annotations menu remains grayed out after completing these steps. When I rebuilt my project it deleted my src directory since I had tried to point the generated classes folder to my src folder so that the generated classes would be side by side with the entity classes. so I can check them in. There should definitely be a warning that it deletes that directory!

    I’m not sure what IDEA is setting the processors working directory to but in the case of Eclipselink I have one processor run option set like so:

    eclipselink.persistencexml=web/META-INF/persistence.xml but when I rebuild the project I get an error saying “Unable to load persistence.xml G:sandboxtrunkmyprojectbuildproductionmyprojectwebMETA-INFpersistence.xml”

    I tried changing the value if the option to be a relative path from the buildproductionmyproject folder back to my persistence.xml file like so:

    eclipselink.persistencexml=../../../web/META-INF/persistence.xml

    but that gave me the following error:

    java.lang.IllegalArguementException: relativeName is invalid

    Has anyone set this up with Eclipselink? BTW, I can generate them by hand on the command link with “javac -processor org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor …” so I know I have my options and classpath information correct and I am just not understanding the magic to make IDEA happy. Thanks,

    -Noah

  9. Gregory Shrago says:

    There is indeed a problem. We are working to make CanonicalModelProcessor happy. It appears to be very restrictive when it comes to paths.

  10. Gregory Shrago says:

    As described here:
    https://bugs.eclipse.org/bugs/show_bug.cgi?id=302450
    EclipseLink processor expects persistence.xml in CLASS_OUTPUT (which is wierd) thus preventing “Process Annotations” action from working correctly but simple “Compile” action works fine if the persistence.xml is under some source root/META-INF/. I’ve checked it with eclipselink.persistencexml=META-INF/persistence.xml option.

    We’ve added the warning about generated_src directory clearing in settings dialog and we have changed the “Process Annotation” action (will be available in the next EAP).
    One should never mix real sources and generated ones otherwise it is not possible to “rebuild all” correctly.

  11. Noah says:

    Gregory,

    Thanks for the information. I’ll try the simple “Compile” action. Your point is taken about mixing the generated and static sources. Things could get silently out-of-date if you don’t have the auto generation wired up via ant/ipr/maven or some other automatic mechanism when you compile.

    -Noah

  12. Gregory Shrago says:

    Annotation Processing is integrated into compiler pipeline thus things could not get out-of-date silently as regeneration is performed on each comile/make/build all. The whole purpose of a separate “Process Annotations” action is to forcibly regenerate sources when Java compilation is undesirable i.e. a project doesn’t compile but we would like to have completion/navigation/etc. for JPA metamodel.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>