Wednesday, February 1, 2012

JPA 2.0 Metamodel generation using Eclipse Maven build

Update:
Please see this updated post in that manner which simplifies the process a bit better.


In order to write JPA2.0 queries using the criteria API in a type safe manner, one can use the corresponding @Entity generated metamodel.

This post is for developers who are familiar with this concept, and feel their current metamodel files generation process need improvement.

Over the web there are some guidelines regarding the best way to generate these static metamodel classes.
However, maybe it's just me, but I could not run them in a proper way.
Some methods generated the metamodel classes in the target directory and some simply did not work.
When running the metamodel generation tool, I had to set the pom file with the proc parameter manually with the "only" value and revert it when I wanted to generate the other java files.
(Very tedious when rapid model changes  are needed)

I would like to suggest a way to create the static metamodel files in the source directory (right beside the @Entities they refer)  AND run the build without modifying the pom/maven file.

The process is set from two steps:
1) Configure the POM.xml
2) Configure eclipse to run the build

So, first thing first - lets configure the pom file:




            org.hibernate

            hibernate-jpamodelgen

            1.1.1.Final

        

    

    

        

            

                maven-clean-plugin

                2.4.1

                

                  

                       

                     ../your_project_name/src/main/java/com/your_path/model/                     

                      

                        **/*_.java                       

                                           

                      false

                    

                  

                

              

           

            

                org.apache.maven.plugins

                maven-compiler-plugin

                2.3.2

                

                    1.7

                    1.7                                           

                    

                        org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor

                    

                    

                        ../your_project_name/src/main/java/

                    

                

            

        

    


The artifact is needed for the @Entity classes analysis and static Metamodel generation.
The maven-clean section is to clean the previous metamodel files (pattern: *_.java ) before generation (useful since the metamodel generation raise an error if any previous file already exist).
The maven-compileer section refer to the class with the metamodel generation class and set the target directory as the source directory (not the target!).

Next step is to define the eclipse maven build.
You can follow the below screenshots (sample build for project called "management"):


Configure the metamodel build:
Configure the project build:

  • For every "regular" build simply run the project build.
  • For any change in the @Entity files, run the compile metamodel build.

That's it..

Important note:
If you know how to run the two builds automatically one after the other, kindly leave a comment.