How to generate JPA 2.0 metamodel?

2019-01-03 04:41发布

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

5条回答
冷血范
2楼-- · 2019-01-03 05:05

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

查看更多
Explosion°爆炸
3楼-- · 2019-01-03 05:17

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.

  1. Select your project in the Package Explorer
  2. Go to Properties -> JPA dialog
  3. Select source folder from Canonical metamodel (JPA 2.0) group
  4. Click Apply button to generate metamodel classes in the selected source folder

enter image description here

This should work on any JPA provider as the generated classes are standard.

Also see here.

查看更多
Melony?
4楼-- · 2019-01-03 05:22

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>
查看更多
冷血范
5楼-- · 2019-01-03 05:27

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

OpenJPA

DataNucleus


The latest Hibernate implementation is available at:

An older Hibernate implementation is at:

查看更多
来,给爷笑一个
6楼-- · 2019-01-03 05:28

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.

查看更多
登录 后发表回答