Idea logo

The IntelliJ IDEA Blog

The Capable & Ergonomic Java IDE by JetBrains


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.

Comments below can no longer be edited.

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

  1. David Vallner says:

    November 19, 2009

    Will this also work with annotation processors that emit modified existing classes? (Notably Project Lombok: )

  2. Gregory Shrago says:

    November 19, 2009

    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. B. K. Oxley (binkley) says:

    November 19, 2009

    I have the same question as David Vallner, re:

    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:

    November 26, 2009

    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. Timo Westkämper says:

    November 26, 2009

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

    • Larry says:

      July 16, 2015


  6. Gregory Shrago says:

    November 30, 2009

    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:

    December 12, 2009

    Does anybody know how to debug annotation processors in IDEA?

  8. Noah says:

    March 2, 2010


    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:


    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,


  9. Gregory Shrago says:

    March 3, 2010

    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:

    March 4, 2010

    As described here:
    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:

    March 4, 2010


    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.


  12. Gregory Shrago says:

    March 9, 2010

    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.

  13. Patrick says:

    May 29, 2014

    This information is terribly outdated. FIX IT!

  14. Ted says:

    August 14, 2014

    Does any one know how to force generation of a single class in IntelliJ 13.x?

    +1 for FIX IT! (the docu)

Discover more