Hibernate静态元模型生成器既能够经过命令行使用,也能够集成在IDE中使用。大多数状况下,若是使用了jdk6及以上的版本,而且注解处理器的jar已经被包含在classpath中,注解处理器会自动的运行,由于Hibernate静态元模型生成器的jar包的META-INF/services目录里已经包含了文件javax.annotation.processing.Processor。 java
在maven构建的过程当中,有几种方式能够运行注解处理器。其一,就是上面提到的在classpath中引入它的jar包。若是在classpath中有多个注解处理器,能够在maven的编译插件中传入处理选项参数 api
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <compilerArguments> <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> </compilerArguments> </configuration> </plugin>使用maven-compiler-plugin的方式的缺点是maven编译插件当前不容许指定多个编译参数。一个比较好的方式是在编译插件中禁用注解处理器,使用maven-processor-plugin。能够按照以下方式禁用 maven-compiler-plugin:
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-proc:none</compilerArgument> </configuration> </plugin>
按照上面处理以后,就能够在maven-processor-plugin中使用注解处理器。能够按照以下配置: eclipse
<plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>2.0.5</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>4.3.5.Final</version> </dependency> </dependencies> </plugin>
在Galileo版本以后,在eclipse的Java Compiler(右键点击项目->属性)里增长了一个名为Annotation Processing的选项,用于配置各类各样的注解处理。打开Annotation Processing的配置界面,勾选“Enable Annotation Processing”,配置代码生成的目录,而后,切换到下面的“Factory Path”配置界面,点击Add Jars,将JPA2.0和hibernate-jpamodelgen的jar包引入,点击Apply,便可: maven
———————————————————————————————————————————————————————————————— spa
hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate