JPA 2 MetaModel Generation using Eclipse

JPA 2.0 introduces the strongly typed Criteria API for going away with the Java Persistence Query (JQL) language. It comes with the metamodel for helping the typed objects. Every entity must have a meta model generated with underscore(_) added to its name. These objects will be referred in the Criteria API instead of using the string based names. These files can be created manually. But, it will impact the productivity and consumes lot of effort for writing every file.

If you remember, Java 6.0 has nice feature known as annotation processing. This feature is utilized and JPA vendors have created the tools for generating metamodel by parsing the entity classes. It is fully automated once you run the tool. Here I am going to explain how you can use eclipse to generate the meta model class for you.

Create Eclipse Project and Classes

Create eclipse project for running this example tutorial for generating the metamodel classes. As a first step, create eclipse Java project and create the classes, and


import java.math.BigDecimal;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity public class Employee {
    Integer id;
    Branch branches;
    Set<Device> devices;
    BigDecimal salary;

public class Device {}

public class Branch {}

Look at the below project structure how it looks.

JPA MetaModel Project Example

JAR files added are:

  • ibernate-jpamodelgen-4.3.0.Final.jar
  • javax.persistence_2.0.0.v200911271158.jar
  • hibernate-jpa-2.1-api-1.0.0.Final.jar

Get JPA MetaModel Generator Tool

It is API provided by most of the JPA vendors for generating the meta data. Here I have used the hibernate-jpamodelgen-4.3.0.Final.jar, which is donwloaded from the hibernate project. Also you can get it from the example code for this tutorial at bottom.

Get JPA Implementation API

It is the actual JPA implementation. Note that, it can be specific for the ORM vendor. Every vendor has their own implementation for JPA. Here I have downloaded both, hibernate and official JPA implementation JAr files for this example tutorial.

Enable Annotation Processing In Your Eclipse Project

This feature is added from Eclipse 3.2 (Galilio). It is project level feature. You can right click on the project and click properties menu. You will get the following window. Enable the “project specific settings”, it will enable the annotation processing for that project. Also you can change source directory where the generated source code will be copied.

Select the “Factory Path” menu in the left side and add the meta model generator JAR file by selecting “Add External JAR”. After this change, when you click on OK, the project will built and the new folder would have created with the metamodel class. You can check in the above project folder structure for knowing where it will be generated.

JPA MetaModel Eclipse

JPA MetaModel Eclipse 2

Generated MetaModel Class

Once the above steps are completed, the below class will be generated.


import java.math.BigDecimal;
import javax.annotation.Generated;
import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
public abstract class Employee_ {

	public static volatile SingularAttribute<Employee, Integer> id;
	public static volatile SingularAttribute<Employee, BigDecimal> salary;
	public static volatile SetAttribute<Employee, Device> devices;
	public static volatile SingularAttribute<Employee, Branch> branches;


How to use these metamodel in Criteria API will be written in the next articles. Please stay tuned to get the more articles on JPA 2.

Download Source Code For This Example : JPAApp

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!