JPA Hibernate通过maven生成元模型 [英] JPA Hibernate Metamodel generation through maven

查看:259
本文介绍了JPA Hibernate通过maven生成元模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循JPA modelgen 指南,我能够生成我需要的规范元模型。设置这个pom:

 < plugin> 
< artifactId> maven-compiler-plugin< / artifactId>
<配置>
< source> 1.6< / source>
< target> 1.6< / target>
< compilerArgument> -proc:none< / compilerArgument>
< / configuration>
< / plugin>
< plugin>
< groupId> org.bsc.maven< / groupId>
< artifactId> maven-processor-plugin< / artifactId>
< version> 2.0.6-redhat< / version>
<执行次数>
<执行>
< id>程序< / id>
<目标>
< goal>程序< / goal>
< /目标>
<阶段> generate-sources< / phase>
<配置>
< outputDirectory>目标/元模型< / outputDirectory>
< / configuration>
< /执行>
< /执行次数>
< / plugin>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> build-helper-maven-plugin< / artifactId>
< version> 1.3< / version>
<执行次数>
<执行>
< id> add-source< / id>
<阶段> generate-sources< / phase>
<目标>
< goal> add-source< / goal>
< /目标>
<配置>
< sources>
< source> target /元模型< / source>
< / sources>
< / configuration>
< /执行>
< /执行次数>
< / plugin>

生成的源在指定的目录中正确创建,我必须手动将其指定为源使用它的eclipse项目类路径。当我触发一个maven时,日志显示找不到符号重复的类,并且我仍然可以成功构建。所以我的问题是,创建元模型的这种预期/正确的行为?还是我错过了cofig的东西?谢谢

解决方案

我也使用JPA Metamodel生成器,我没有描述的问题,也许我的配置可以帮助,我看到一些差异,第一个是 maven-processor-plugin

 <插件> 
< groupId> org.bsc.maven< / groupId>
< artifactId> maven-processor-plugin< / artifactId>
< version> 2.1.0< / version>
<执行次数>
<执行>
< id>程序< / id>
<目标>
< goal>程序< / goal>
< /目标>
<阶段> generate-sources< / phase>
<配置>
<处理器>
< processor> org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor< / processor>
< /处理器>
< / configuration>
< /执行>
< /执行次数>
<依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-jpamodelgen< / artifactId>
<! - version> 1.2.0.Final< / version - >
< version> 4.3.4.Final< / version>
< /依赖关系>
< /依赖关系>
< / plugin>

正如您所看到的,我必须添加 hibernate-jpamodelgen

我不确定是否 build-helper-maven-plugin 有必要将生成的源代码目录添加到源路径中。我没有使用它,它适用于我,但也许这是因为我使用默认输出路径生成源。


I followed the JPA modelgen guide and i was able to generate the canonical metamodel which i need. With this pom set up:

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.6-redhat</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <outputDirectory>target/metamodel</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/metamodel</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

The generated source is properly created in the specified directory and i have to manually specify it as a source in the eclipse project class path to use it. When i trigger a maven the logs show cannot find symbol or duplicate class and i still get successful build. So my question is, is this expected/correct behavior in creating the metamodel? or did i miss something in the cofig? Thanks

解决方案

I'm also using JPA Metamodel generator and I don't have the problems you describe, maybe my configuration can help, I see some differences, the first one is maven-processor-plugin

<plugin>
  <groupId>org.bsc.maven</groupId>
  <artifactId>maven-processor-plugin</artifactId>
  <version>2.1.0</version>
  <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>1.2.0.Final</version-->
      <version>4.3.4.Final</version>
    </dependency>
  </dependencies>
</plugin>

As you can see I had to add hibernate-jpamodelgen as dependency and the processor attribute.

I am not sure if build-helper-maven-plugin is necessary to add the directory of generated sources to your source path. I am not using it and it works for me but maybe it's because I am using the default output path for generated sources.

这篇关于JPA Hibernate通过maven生成元模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆