如何生成 JPA 实体元模型?

本着与 CriteriaQuery JPA 2.0 相关的类型安全精神,JPA 2.0 还具有一个 API 来支持实体的 Metamodel 表示。

有没有人知道这个API的全功能实现(生成元模型,而不是手动创建元模型类)?如果有人也知道在Eclipse中设置它的步骤,那就太好了(我假设它就像设置注释处理器一样简单,但你永远不会知道)。

编辑:刚刚偶然发现了Hibernate JPA 2元模型生成器。但问题仍然存在,因为我找不到jar的任何下载链接。

编辑2:自从我问这个问题以来已经过去了一段时间,但我想我会回来并在SourceForge上添加一个指向Hibernate JPA模型生成器项目的链接


答案 1

如果有人也知道在Eclipse中设置它的步骤,那就太好了(我假设它就像设置注释处理器一样简单,但你永远不会知道)

是的,它是。以下是各种 JPA 2.0 实现的实现和说明:

EclipseLink

冬眠

OpenJPA

DataNucleus


最新的 Hibernate 实现可在以下位置获得:

较旧的Hibernate实现位于:


答案 2

请看一下jpa-metamodels-with-maven-example

冬眠

  • 我们需要.org.hibernate.org:hibernate-jpamodelgen
  • 处理器类为 。org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor

休眠作为依赖项

    <dependency>
      <groupId>org.hibernate.orm</groupId>
      <artifactId>hibernate-jpamodelgen</artifactId>
      <version>${version.hibernate-jpamodelgen}</version>
      <scope>provided</scope>
    </dependency>

作为处理器休眠

      <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <compilerArguments>-AaddGeneratedAnnotation=false</compilerArguments> <!-- suppress java.annotation -->
              <processors>
                <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
              </processors>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${version.hibernate-jpamodelgen}</version>
          </dependency>
        </dependencies>
      </plugin>

OpenJPA

  • 我们需要.org.apache.openjpa:openjpa
  • 处理器类为 。org.apache.openjpa.persistence.meta.AnnotationProcessor6
  • OpenJPA似乎需要额外的元素。<openjpa.metamodel>true<openjpa.metamodel>

OpenJPA 作为依赖项

  <dependencies>
    <dependency>
      <groupId>org.apache.openjpa</groupId>
      <artifactId>openjpa</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <compilerArgs>
            <arg>-Aopenjpa.metamodel=true</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>

OpenJPA 作为处理器

      <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>${version.openjpa}</version>
          </dependency>
        </dependencies>
      </plugin>

EclipseLink

  • 我们需要.org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor
  • 处理器类为 。org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
  • EclipseLink 需要 .persistence.xml

EclipseLink 作为依赖项

  <dependencies>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
      <scope>provided</scope>
    </dependency>

EclipseLink 作为处理器

    <plugins>
      <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <processors>
                <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
              </processors>
              <compilerArguments>-Aeclipselink.persistencexml=src/main/resources-${environment.id}/META-INF/persistence.xml</compilerArguments>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>${version.eclipselink}</version>
          </dependency>
        </dependencies>
      </plugin>

DataNucleus

  • 我们需要.org.datanucleus:datanucleus-jpa-query
  • 处理器类为 。org.datanucleus.jpa.query.JPACriteriaProcessor

数据核作为依赖关系

  <dependencies>
    <dependency>
      <groupId>org.datanucleus</groupId>
      <artifactId>datanucleus-jpa-query</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>

作为处理器的数据核

      <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.datanucleus.jpa.query.JPACriteriaProcessor</processor>
              </processors>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-jpa-query</artifactId>
            <version>${version.datanucleus}</version>
          </dependency>
        </dependencies>
      </plugin>

推荐