简体   繁体   中英

How to use Hibernate JPA 2 Metamodel Generator?

When I learned Hibernate JPA 2 Metamodel Generator according to http://docs.jboss.org/hibernate/stable/jpamodelgen/reference/en-US/html_single/ , it is working all right.
But when I tried to generate these metamodel by run mvn compile , it only generated corresponding classes and a strange folder 'generated-sources' in target folder as below and does not generated corresponding java files in source folder. 在此处输入图片说明

Below is my related configuration in pom.xml:

    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerArgument>-proc:none</compilerArgument>
    </configuration>
</plugin>
<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.2.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>
</plugin>
</plugins>


So, my question is : is this expected behavior? If yes, does it mean that I need to compile code before I use metamodel each time?. If no, how can I generate java files in source code folder?

Thank you for your help in advance.

But when I tried to generate these metamodel by run mvn compile, it only generated corresponding classes and a strange folder 'generated-sources' in target folder as below and does not generated corresponding java files in source folder.

The default folder for the generated meta classes is ${project.build.directory}/generated-sources/apt where ${project.build.directory} is target by default. So the generated meta classes should be under target/generated-sources/apt directory (as I can guess from your screenshot the meta classes are generated).

If you want to change this behavior you can configure the plugin to generate the meta classes to another folder by using the outputDirectory element as follows:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.2.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>
             <outputDirectory>${project.basedir}/generated</outputDirectory>
        </configuration>
    </execution>
</executions>

And don't forget to add the new folder into your class path otherwise your project might not build.

is this expected behavior?

yes

If yes, does it mean that I need to compile code before I use metamodel each time?

yes, but if you don't change or add new entities you can use the generaed classes provided you run mvn compile at least once.

If no, how can I generate java files in source code folder?

I don't recommend to do it :-) I mean mixing the generated classes with the source code is not a good idea. If you don't like the target folder use any other folder as described above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM