Search code examples
javajpajakarta-eecriteria

Why are all fields null in JPA Metamodel?


I am just learning JPA criteria and metamodels. I found the following example of a metamodel:

@StaticMetamodel( Person.class )
public class Person_ {
    public static volatile SingularAttribute<Person, Long> id;
    public static volatile SingularAttribute<Person, String> name;
    public static volatile SingularAttribute<Person, Integer> age;
    public static volatile SingularAttribute<Person, Address> address;
    public static volatile SetAttribute<Person, Order> orders;
}

All fields of this metamodel are null. So, could anyone explain how JPA provider will get, for example, field name in the following example:

criteria.where(builder.equal( personRoot.get( Person_.age ), 50));

Solution

  • The fields are not null at runtime, because the JPA implementation sets all those public static fields to non-null values.

    Excerpt from the specifications (6.2.2 - Bootstrapping):

    When the entity manager factory for a persistence unit is created, it is the responsibility of the persistence provider to initialize the state of the metamodel classes of the persistence unit.