In the spirit of type safety associated with the CriteriaQuery JPA 2.0 also has an API to support Metamodel representation of entities.
Is anyone aware of a fully functional implementation of this API (to generate the Metamodel as opposed to creating the metamodel classes manually)? It would be awesome if someone also knows the steps for setting this up in Eclipse (I assume it's as simple as setting up an annotation processor, but you never know).
EDIT: Just stumbled across Hibernate JPA 2 Metamodel Generator . But the issue remains since I can't find any download links for the jar.
EDIT 2: Awhile has passed since I asked this question, but I thought I'd come back and add a link to the Hibernate JPA Model Generator project on SourceForge
It would be awesome if someone also knows the steps for setting this up in Eclipse (I assume it's as simple as setting up an annotation processor, but you never know)
Yes it is. Here are the implementations and instructions for the various JPA 2.0 implementations:
EclipseLink
Hibernate
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
- http://in.relation.to/2009/11/09/hibernate-static-metamodel-generator-annotation-processor
OpenJPA
org.apache.openjpa.persistence.meta.AnnotationProcessor6
- http://openjpa.apache.org/builds/2.4.1/apache-openjpa/docs/ch13s04.html
DataNucleus
org.datanucleus.jpa.JPACriteriaProcessor
- http://www.datanucleus.org/products/accessplatform_2_1/jpa/jpql_criteria_metamodel.html
The latest Hibernate implementation is available at:
An older Hibernate implementation is at:
REVISED (March/2014)
Please take a look at jpa-metamodels-with-maven
Hibernate
Using Hibernate is most recommended.
(I'm not judging any features/functions/usabilities/stabilites on those implementations. And the above statement is only focused in maven usage that I constructed.)
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>4.3.4.Final</version>
</dependency>
</dependencies>
</plugin>
OpenJPA
OpenJPA seems require additional element <openjpa.metamodel>true<openjpa.metamodel>
.
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.apache.openjpa.persistence.meta.AnnotationProcessor6</processor>
</processors>
<optionMap>
<openjpa.metamodel>true</openjpa.metamodel>
</optionMap>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</plugin>
EclipseLink
EclipseLink requires persistence.xml
.
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
</plugin>
========================================
For Apache Maven users,
Following simple configuration seems work. (with old maven-compiler-plugin; updated.)
<!-- This method doesn't work with newest maven-compiler-plugin -->
<!-- But if it's ok to work with old maven-compiler-plugin -->
<!-- This is the best method -->
<!-- There is no other required configurations -->
<!-- We don't even require to config any processor names -->
<project>
<build>
<extensions>
<extension>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.3.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version> <!-- this is critical -->
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
you can run it with "mvn compiler:compile"
UPDATE
Note that this method works only with those old maven-compiler-plugin. Check the version in code.
Eclipse's JPA 2.0 support through Dali (which is included in "Eclipse IDE for JEE Developers") has its own metamodel generator integrated with Eclipse.
- Select your project in the Package Explorer
- Go to Properties -> JPA dialog
- Select source folder from Canonical metamodel (JPA 2.0) group
- Click Apply button to generate metamodel classes in the selected source folder

This should work on any JPA provider as the generated classes are standard.
Also see here.
For eclipselink, only the following dependency is sufficient to generate metamodel. Nothing else is needed.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
For Hibernate as provider which is most common IMHO:
In case of build tools like Gradle, Maven you need to have Hibernate JPA 2 Metamodel Generator jar in the classpath and compiler level>=1.6 that is all you need build the project and metamodel will be generated automatically.
In case of IDE Eclipse 1. goto Project->Properties->Java Compiler->Annotation Processing and enable it. 2. Expand Annotation Processing->Factory Path-> Add External Jar add Hibernate JPA 2 Metamodel Generator jar check the newly added jar and say OK. Clean and Build done!
Link Hibernate JPA 2 Metamodel Generator jar link from maven repo https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen
来源:https://stackoverflow.com/questions/3037593/how-to-generate-jpa-2-0-metamodel