Module jakarta.data

Annotation Interface StaticMetamodel


@Documented @Retention(RUNTIME) @Target(TYPE) public @interface StaticMetamodel

Annotates a class which serves as a static metamodel for an entity, enabling type-safe access to entity attribute names and related objects such as instances of Sorts for an attribute. A metamodel class contains one or more public static fields corresponding to persistent fields of the entity class. The type of each of these fields must be either String, Attribute, or a subinterface of Attribute defined in this package.

Jakarta Data defines the following conventions for static metamodel classes:

  • The name of the static metamodel class should consist of underscore (_) followed by the entity class name.
  • Fields of type String should be named with all upper case.
  • Fields of type Attribute should be named in lower case or mixed case.

For example, for the following entity,

 @Entity
 public class Person {
     @Id
     public long ssn;

     @Embedded
     public Name name;

     public int yearOfBirth;
 }

 @Embeddable
 public class Name {
     public String first;
     public String last;
 }
 

An application programmer may define a static metamodel as follows,

 @StaticMetamodel(Person.class)
 public class _Person {
     // These can be uninitialized and non-final if you don't need to access them from annotations.
     public static final String SSN = "ssn";
     public static final String NAME = "name";
     public static final String NAME_FIRST = "name.first";
     public static final String NAME_LAST = "name.last";
     public static final String YEAROFBIRTH = "yearOfBirth";

     public static final SortableAttribute<Person> ssn = new SortableAttributeRecord<>("ssn");
     public static final Attribute<Person> name = new AttributeRecord<>("name");
     public static final TextAttribute<Person> name_first = new TextAttributeRecord<>("name.first");
     public static final TextAttribute<Person> name_last = new TextAttributeRecord<>("name.last");
     public static final SortableAttribute<Person> yearOfBirth = new SortableAttributeRecord<>("yearOfBirth");
 }
 

And use it to refer to entity attributes in a type-safe manner,

 PageRequest<Product> pageRequest = Order.by(_Person.yearOfBirth.desc(),
                                             _Person.name_last.asc(),
                                             _Person.name_first.asc(),
                                             _Person.ssn.asc())
                                         .page(1)
                                         .size(20);
 

Alternatively, an annotation processor might generate static metamodel classes for entities at compile time. The generated classes must be annotated with the @Generated annotation. The fields may be statically initialized, or they may be initialized by the provider during system initialization. In the first case, the fields are declared final. In the second case, the fields are declared non-final and volatile.

In cases where multiple Jakarta Data providers provide repositories for the same entity type, no guarantees are made of the order in which the Jakarta Data providers attempt to initialize the fields of the static metamodel class for that entity.