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.

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.

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.

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.

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,

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.

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.

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.