Annotation Interface StaticMetamodel


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

Annotates a class to serve as a static metamodel for an entity, enabling type-safe access to entity attribute names and related objects, such as Sorts for an attribute.

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;
 }
 

You can define a static metamodel as follows,

 @StaticMetamodel(Person.class)
 public class Person_ {
     public static final Attribute ssn = Attribute.get(); // ssn or id
     public static final Attribute name = Attribute.get();
     public static final Attribute name_first = Attribute.get();
     public static final Attribute name_last = Attribute.get();
     public static final Attribute yearOfBirth = Attribute.get();
 }
 

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

 pageRequest = Pageable.ofSize(20).sortBy(Person_.yearOfBirth.desc(),
                                          Person_.name_last.asc(),
                                          Person_.name_first.asc(),
                                          Person_.ssn.asc());
 

When a class is annotated as a StaticMetamodel, Jakarta Data providers that provide a repository for the entity type must assign the value of each field that meets the following criteria:

  • The field type is Attribute.
  • The field is public.
  • The field is static.
  • The field is final.
  • The name of the field, ignoring case, matches the name of an entity attribute, where the _ character delimits the attribute names of hierarchical structures such as embedded classes.

The Jakarta Data provider must initialize each Attribute value that corresponds to the name of an entity attribute.

Additionally, a field that meets the above criteria except for the name and is named id must be assigned by the Jakarta Data provider to the unique identifier entity attribute if a single entity attribute represents the unique identifier.

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 initialize the Attribute fields of the class that is annotated with StaticMetamodel.

  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    An entity class.
  • Element Details

    • value

      Class<?> value
      An entity class.
      Returns:
      the entity class.