JPA 2.0 Typesafe Criteria API and Annotation Processing Howto

November 19th, 2009 by Gregory Shrago

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.

Tags: , , , , ,

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

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

    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?

Leave a Reply