In JPA2 there is a query API referred to as "criteria". This is really an API allowing
the construction of queries expression by expression, and optionally making it type safe
so that if you refactor a field name then it is changed in the queries. One mechanism for
allowing refactoring of queries is to have something called a
static metamodel
of generated classes that mirror the applications persistable classes and have persistable
fields marked as
public
and
static
so that they can be accessed when generating
the queries.
The JPA2 spec contains the following description of the static metamodel.
For every managed class in the persistence unit, a corresponding metamodel class is produced
as follows:
Let's take an example, for the following class
package org.datanucleus.samples.jpa2.metamodel;
import java.util.*;
import javax.persistence.*;
@Entity
public class Person
{
@Id
long id;
String name;
@OneToMany
List<Address> addresses;
}
the static metamodel class will be
package org.datanucleus.samples.jpa2.metamodel;
import javax.persistence.metamodel.*;
@StaticMetamodel(Person.class)
public class Person_
{
public static volatile SingularAttribute<Person, Long> id;
public static volatile SingularAttribute<Person, String> name;
public static volatile ListAttribute<Person, Address> addresses;
}
The DataNucleus JPA plugin contains an
annotation processor
that can be used
when compiling your model classes to generate the static metamodel classes. To enable this
you need to be using JDK1.6+ and to provide the compiler command line argument
-processor org.datanucleus.jpa.JPACriteriaProcessor
To enable this in Maven2 you would need the following in your POM
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<processor>org.datanucleus.jpa.JPACriteriaProcessor</processor>
</compilerArguments>
</configuration>
</plugin>
This will then create the static metamodel source files in the build area, and then
compile them.