Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Hibernate Metamodel generation through maven

Tags:

hibernate

jpa

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

like image 258
geneqew Avatar asked May 10 '12 05:05

geneqew


People also ask

How do I generate a JPA metamodel class?

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!

What is Staticmetamodel?

A static metamodel is a series of classes that "mirror" the entities and embeddables in the domain model and provide static access to the metadata about the mirrored class's attributes.


1 Answers

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.

like image 154
GAR Avatar answered Dec 30 '22 15:12

GAR