Annotation Interface StaticMetamodel


@Target(TYPE) @Retention(RUNTIME) public @interface StaticMetamodel
The StaticMetamodel annotation specifies that the class is a metamodel class that represents the Java type designated by the value() element. The Java type might be:

Typically, a static metamodel class is generated by an annotation processor according to the specification of the Jakarta Persistence canonical metamodel. Metamodel classes which do not conform to this specification are not portable between persistence providers.

The canonical metamodel for a class X belongs to the same package as X, and is named X_.

Suppose the persistence unit contains the following entity class:

package org.example;
import jakarta.persistence.*;

@Entity class Book {
    @Id String isbn;
    ...
}

Then the canonical metamodel class is org.example.Book_.

import org.example.*;
import jakarta.persistence.*;
...
    EntityType<Book> bookType = Book_.class_;
    SingularAttribute<Book,String> isbn = Book_.isbn;

Similarly, suppose the persistence unit contains the following interface annotated @NamedQuery:

package com.acme.orders;
import jakarta.persistence.*;

@NamedQuery(name = "allOrdersBySku",
            query = "select o from Order o join i.items i where i.sku = ?1")
...
interface OrderQueries { ... }

Then the canonical metamodel class is com.acme.orders.OrderQueries_.

import com.acme.orders.*;
import jakarta.persistence.*;
...
    var ordersForSku =
            entityManager.createQuery(OrderQueries_._allOrdersBySku_)
                    .setParameter(1, sku)
                    .getResultList();

Note that the canonical metamodel field for the named query allOrdersBySku is named _allOrdersBySku_ to avoid naming collisions.

Since:
2.0
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Class being modelled by the annotated class.
  • Element Details

    • value

      @Nonnull Class<?> value
      Class being modelled by the annotated class.