Skip to main content

Generating the JPA 2.0 Static MetaModel classes using Eclipselink 2.0 and Netbeans 6.8 for your Entity Classes

Posted by lancea on December 15, 2009 at 1:11 PM PST

Eclipselink 2.0 includes an annotation processor for generating the static metamodel classes from your Entity classes for use with the Criteria API which is one of the new features in JPA 2.0.

Here is an example of generating the metamodel classes from the command line using the Eclipselink annotation processor:


javac -processor org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor -sourcepath src -d src -classpath /ace2_apps/eclipselink/jlib/eclipselink.jar:.:/ace2_apps/eclipselink/jlib/JPA/javax.persistence_2.0.0.v200911271158.jar -proc:only -Aeclipselink.persistencexml=src/META-INF/persistence.xml src/demo/*.java
Note: Creating the metadata factory ...
Note: Building metadata class for round element: demo.Item
Note: Building metadata class for round element: demo.Order
Note: Found Option : eclipselink.persistencexml, with value: src/META-INF/persistence.xml
Note: File was not found: META-INF/orm.xml
Note: File was not found: META-INF/eclipselink-orm.xml
Note: Found Option : eclipselink.persistencexml, with value: src/META-INF/persistence.xml
Note: File was not found: META-INF/orm.xml
Note: File was not found: META-INF/eclipselink-orm.xml
warning: The following options were not recognized by any processor: '[eclipselink.persistencexml]'



In the above example, the metamodel classes demo.Item_.java and demo.Order_.java were generated and placed in the demo directory where the Entity classes reside.

Netbeans 6.8 has recently been released which provides support for many Java EE 6 features. Unfortunately, there is no support for generating the metamodel classes at this time.

However, it is easy to update your project to provide this support following these steps:

1. Add the following target to your projects build.xml file:




   


    destdir="${src.dir}"
    excludes="demo/**"
    classpath="${javac.classpath}">
   
   



A couple of notes about the target above:

  • Each time you want to regenerate the metamodel classes, you must delete the previous ones or you will receive an error.
  • The target excludes running the annotation processor against the directory that has the code which references the metamodel classes (demo in the target) otherwise you will see some errors yet the metamodel classes will still be generated.

Once you have added the target to the build.xml, you will want to add a menu option to make it easier to invoke the target. You can do this by:

1. Right clicking on the name of the target and "Create Shortcut"

CreateMenuItem.JPG

2. Indicate that you are adding a "Menu Item"

Wizard1.JPG

3. Select "Refactor" for where to add the Menu Item.

Wizard2.JPG

Now you are ready to use the new target which you can access from the refactor menu:

ShowMenuItem.JPG

The Netbeans project, CriteriaStaticMetaModelDemo, provides a simple set of examples on how to use the Criteria AP. The queries that are provided access the metamodel dynamically and statically as well as demonstrate String based navigation.

The code for using the static metamodel example looks like:

private void staticMetaModel() {
System.out.println("***Running static metamodel Example*****");
CriteriaBuilder cb = em.getCriteriaBuilder();

CriteriaQuery cq = cb.createQuery(League.class);

Root league = cq.from(League.class);

cq.select(league);

ParameterExpression genderType =

cb.parameter(Gender.class, "gender");
cq.where(cb.equal(league.get(League_.gender), genderType));
TypedQuery tq = em.createQuery(cq);

tq.setParameter(genderType, Gender.Mixed);

List rs = tq.getResultList();

displayLeagues(rs);
CriteriaQuery cq2 = cb.createQuery(String.class);

Root team = cq2.from(Team.class);

cq2.select(team.get(Team_.name)).where(cb.like(team.get(Team_.name), "Longfellow%"));

TypedQuery tq2 = em.createQuery(cq2);

List teamNames = tq2.getResultList();

displayTeamNames(teamNames);

}


The example creates two queries using the Criteria API. The first query will select all leagues whose gender is equal to "Mixed". Gender is a Enum.

The 2nd query will return all teams whose name begins with "Longfellow".

The static metamodel classes are generated by appending an "_" to the name of the entity. In the example above, League_ and Team_ are the static metamodel classes that were generated for the entities League and Team.

Both queries are quite trivial but show how you can create strongly typed queries using the static metamodel.


For more information on JPA 2.0:

[1] JPA 2.0 Specification

[2]Eclipselink metamodel Annotation Processor Documentation

[3] CriteriaStaticMetaModelDemo

Related Topics >>