Jakarta Persistence API provides a comprehensive framework for object-relational mapping, entity lifecycle management, and database operations in Java applications
Complete reference for accessing entity metadata and type-safe queries using the Metamodel API.
import jakarta.persistence.metamodel.*;Access metadata for managed types, entities, and embeddables.
/**
* Provides access to the metamodel of persistent entities
* @since 2.0
*/
public interface Metamodel {
/**
* Return the metamodel entity type representing the entity
* @param cls the type of the represented entity
* @return the metamodel entity type
*/
<X> EntityType<X> entity(Class<X> cls);
/**
* Return the metamodel managed type representing the managed type
* @param cls the type of the represented managed type
* @return the metamodel managed type
*/
<X> ManagedType<X> managedType(Class<X> cls);
/**
* Return the metamodel embeddable type representing the embeddable class
* @param cls the type of the represented embeddable type
* @return the metamodel embeddable type
*/
<X> EmbeddableType<X> embeddable(Class<X> cls);
/**
* Return the metamodel managed types
* @return the metamodel managed types
*/
Set<ManagedType<?>> getManagedTypes();
/**
* Return the metamodel entity types
* @return the metamodel entity types
*/
Set<EntityType<?>> getEntities();
/**
* Return the metamodel embeddable types
* @return the metamodel embeddable types
*/
Set<EmbeddableType<?>> getEmbeddables();
}Usage Example:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
Metamodel metamodel = emf.getMetamodel();
// Get entity type
EntityType<Customer> customerType = metamodel.entity(Customer.class);
String entityName = customerType.getName();
// Get all entities
Set<EntityType<?>> entities = metamodel.getEntities();
for (EntityType<?> entity : entities) {
System.out.println("Entity: " + entity.getName());
}Base interfaces for representing type information.
/**
* Instances of the type Type represent persistent object or attribute types
* @since 2.0
*/
public interface Type<X> {
/**
* Persistence type enumeration
*/
public static enum PersistenceType {
/** Entity */
ENTITY,
/** Embeddable class */
EMBEDDABLE,
/** Mapped superclass */
MAPPED_SUPERCLASS,
/** Basic type */
BASIC
}
/**
* Return the persistence type
* @return persistence type
*/
PersistenceType getPersistenceType();
/**
* Return the represented Java type
* @return Java type
*/
Class<X> getJavaType();
}
/**
* Instances of BasicType represent basic types (including temporal and enumerated types)
* @since 2.0
*/
public interface BasicType<X> extends Type<X> {
}
/**
* Instances of Bindable represent object or attribute types that can be bound into a Path
* @since 2.0
*/
public interface Bindable<T> {
/**
* Bindable type enumeration
*/
public static enum BindableType {
/** Single-valued attribute type */
SINGULAR_ATTRIBUTE,
/** Multi-valued attribute type */
PLURAL_ATTRIBUTE,
/** Entity type */
ENTITY_TYPE
}
/**
* Return the bindable type of the represented object
* @return bindable type
*/
BindableType getBindableType();
/**
* Return the Java type of the represented object
* If the bindable type is PLURAL_ATTRIBUTE, the Java element type is returned
* @return Java type
*/
Class<T> getBindableJavaType();
}Interfaces for entity, embeddable, and mapped superclass types.
/**
* Instances represent managed types (entity, mapped superclass, or embeddable)
* @since 2.0
*/
public interface ManagedType<X> extends Type<X> {
/**
* Return the attributes of the managed type
* @return attributes
*/
Set<Attribute<? super X, ?>> getAttributes();
/**
* Return the attributes declared by the managed type
* @return declared attributes
*/
Set<Attribute<X, ?>> getDeclaredAttributes();
/**
* Return the single-valued attribute of the given name and type
* @param name the name of the represented attribute
* @param type the type of the represented attribute
* @return single-valued attribute
*/
<Y> SingularAttribute<? super X, Y> getSingularAttribute(String name, Class<Y> type);
/**
* Return the declared single-valued attribute of the given name and type
* @param name the name of the represented attribute
* @param type the type of the represented attribute
* @return declared single-valued attribute
*/
<Y> SingularAttribute<X, Y> getDeclaredSingularAttribute(String name, Class<Y> type);
/**
* Return the single-valued attributes of the managed type
* @return single-valued attributes
*/
Set<SingularAttribute<? super X, ?>> getSingularAttributes();
/**
* Return the single-valued attributes declared by the managed type
* @return declared single-valued attributes
*/
Set<SingularAttribute<X, ?>> getDeclaredSingularAttributes();
/**
* Return the Collection-valued attribute of the given name and element type
* @param name the name of the represented attribute
* @param elementType the element type of the represented attribute
* @return CollectionAttribute
*/
<E> CollectionAttribute<? super X, E> getCollection(String name, Class<E> elementType);
/**
* Return the Collection-valued attribute declared by the managed type
* @param name the name of the represented attribute
* @param elementType the element type of the represented attribute
* @return declared CollectionAttribute
*/
<E> CollectionAttribute<X, E> getDeclaredCollection(String name, Class<E> elementType);
/**
* Return the Set-valued attribute of the given name and element type
* @param name the name of the represented attribute
* @param elementType the element type of the represented attribute
* @return SetAttribute
*/
<E> SetAttribute<? super X, E> getSet(String name, Class<E> elementType);
/**
* Return the Set-valued attribute declared by the managed type
* @param name the name of the represented attribute
* @param elementType the element type of the represented attribute
* @return declared SetAttribute
*/
<E> SetAttribute<X, E> getDeclaredSet(String name, Class<E> elementType);
/**
* Return the List-valued attribute of the given name and element type
* @param name the name of the represented attribute
* @param elementType the element type of the represented attribute
* @return ListAttribute
*/
<E> ListAttribute<? super X, E> getList(String name, Class<E> elementType);
/**
* Return the List-valued attribute declared by the managed type
* @param name the name of the represented attribute
* @param elementType the element type of the represented attribute
* @return declared ListAttribute
*/
<E> ListAttribute<X, E> getDeclaredList(String name, Class<E> elementType);
/**
* Return the Map-valued attribute of the given name, key type, and value type
* @param name the name of the represented attribute
* @param keyType the key type of the represented attribute
* @param valueType the value type of the represented attribute
* @return MapAttribute
*/
<K, V> MapAttribute<? super X, K, V> getMap(String name, Class<K> keyType, Class<V> valueType);
/**
* Return the Map-valued attribute declared by the managed type
* @param name the name of the represented attribute
* @param keyType the key type of the represented attribute
* @param valueType the value type of the represented attribute
* @return declared MapAttribute
*/
<K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType);
/**
* Return the Collection-valued attributes of the managed type
* @return Collection-valued attributes
*/
Set<PluralAttribute<? super X, ?, ?>> getPluralAttributes();
/**
* Return the Collection-valued attributes declared by the managed type
* @return declared Collection-valued attributes
*/
Set<PluralAttribute<X, ?, ?>> getDeclaredPluralAttributes();
/**
* Return the attribute of the given name
* @param name the name of the represented attribute
* @return attribute
*/
Attribute<? super X, ?> getAttribute(String name);
/**
* Return the declared attribute of the given name
* @param name the name of the represented attribute
* @return declared attribute
*/
Attribute<X, ?> getDeclaredAttribute(String name);
}
/**
* Instances represent entity types
* @since 2.0
*/
public interface EntityType<X> extends IdentifiableType<X>, Bindable<X> {
/**
* Return the entity name
* @return entity name
*/
String getName();
}
/**
* Instances represent embeddable types
* @since 2.0
*/
public interface EmbeddableType<X> extends ManagedType<X> {
}
/**
* Instances represent mapped superclass types
* @since 2.0
*/
public interface MappedSuperclassType<X> extends IdentifiableType<X> {
}
/**
* Instances represent identifiable types (entity or mapped superclass)
* @since 2.0
*/
public interface IdentifiableType<X> extends ManagedType<X> {
/**
* Return the attribute that corresponds to the id attribute of the entity or mapped superclass
* @param type the type of the represented id attribute
* @return id attribute
*/
<Y> SingularAttribute<? super X, Y> getId(Class<Y> type);
/**
* Return the attribute that corresponds to the id attribute declared by the entity or mapped superclass
* @param type the type of the represented declared id attribute
* @return declared id attribute
*/
<Y> SingularAttribute<X, Y> getDeclaredId(Class<Y> type);
/**
* Return the attribute that corresponds to the version attribute of the entity or mapped superclass
* @param type the type of the represented version attribute
* @return version attribute
*/
<Y> SingularAttribute<? super X, Y> getVersion(Class<Y> type);
/**
* Return the attribute that corresponds to the version attribute declared by the entity or mapped superclass
* @param type the type of the represented declared version attribute
* @return declared version attribute
*/
<Y> SingularAttribute<X, Y> getDeclaredVersion(Class<Y> type);
/**
* Return the identifiable type that corresponds to the most specific mapped superclass or entity
* @return supertype
*/
IdentifiableType<? super X> getSupertype();
/**
* Whether the identifiable type has a single id attribute
* @return boolean indicating whether the identifiable type has a single id attribute
*/
boolean hasSingleIdAttribute();
/**
* Whether the identifiable type has a version attribute
* @return boolean indicating whether the identifiable type has a version attribute
*/
boolean hasVersionAttribute();
/**
* Return the attributes corresponding to the id class of the identifiable type
* @return id class attributes
*/
Set<SingularAttribute<? super X, ?>> getIdClassAttributes();
/**
* Return the type that represents the type of the id
* @return type of id
*/
Type<?> getIdType();
}Interfaces for representing entity and embeddable attributes.
/**
* Represents an attribute of a Java type
* @since 2.0
*/
public interface Attribute<X, Y> {
/**
* Persistent attribute type enumeration
*/
public static enum PersistentAttributeType {
/** Many-to-one association */
MANY_TO_ONE,
/** One-to-one association */
ONE_TO_ONE,
/** Basic attribute */
BASIC,
/** Embeddable class attribute */
EMBEDDED,
/** Many-to-many association */
MANY_TO_MANY,
/** One-to-many association */
ONE_TO_MANY,
/** Element collection */
ELEMENT_COLLECTION
}
/**
* Return the name of the attribute
* @return name
*/
String getName();
/**
* Return the persistent attribute type for the attribute
* @return persistent attribute type
*/
PersistentAttributeType getPersistentAttributeType();
/**
* Return the managed type representing the type in which the attribute was declared
* @return declaring type
*/
ManagedType<X> getDeclaringType();
/**
* Return the Java type of the represented attribute
* @return Java type
*/
Class<Y> getJavaType();
/**
* Return the java.lang.reflect.Member for the represented attribute
* @return corresponding java.lang.reflect.Member
*/
java.lang.reflect.Member getJavaMember();
/**
* Is the attribute an association
* @return boolean indicating whether the attribute corresponds to an association
*/
boolean isAssociation();
/**
* Is the attribute collection-valued (represents a Collection, Set, List, or Map)
* @return boolean indicating whether the attribute is collection-valued
*/
boolean isCollection();
}
/**
* Instances represent singular attributes (basic, embedded, many-to-one, one-to-one)
* @since 2.0
*/
public interface SingularAttribute<X, T> extends Attribute<X, T>, Bindable<T> {
/**
* Is the attribute an id attribute
* @return boolean indicating whether the attribute is an id attribute
*/
boolean isId();
/**
* Is the attribute a version attribute
* @return boolean indicating whether the attribute is a version attribute
*/
boolean isVersion();
/**
* Is the attribute optional
* @return boolean indicating whether the attribute is optional
*/
boolean isOptional();
/**
* Return the type that represents the type of the attribute
* @return type of attribute
*/
Type<T> getType();
}
/**
* Instances represent plural attributes (one-to-many, many-to-many, element collections)
* @since 2.0
*/
public interface PluralAttribute<X, C, E> extends Attribute<X, C>, Bindable<E> {
/**
* Collection type enumeration
*/
public static enum CollectionType {
/** Collection */
COLLECTION,
/** Set */
SET,
/** List */
LIST,
/** Map */
MAP
}
/**
* Return the collection type
* @return collection type
*/
CollectionType getCollectionType();
/**
* Return the type representing the element type of the collection
* @return element type
*/
Type<E> getElementType();
}
/**
* Instances represent persistent java.util.Collection-valued attributes
* @since 2.0
*/
public interface CollectionAttribute<X, E> extends PluralAttribute<X, java.util.Collection<E>, E> {
}
/**
* Instances represent persistent java.util.Set-valued attributes
* @since 2.0
*/
public interface SetAttribute<X, E> extends PluralAttribute<X, java.util.Set<E>, E> {
}
/**
* Instances represent persistent java.util.List-valued attributes
* @since 2.0
*/
public interface ListAttribute<X, E> extends PluralAttribute<X, java.util.List<E>, E> {
}
/**
* Instances represent persistent java.util.Map-valued attributes
* @since 2.0
*/
public interface MapAttribute<X, K, V> extends PluralAttribute<X, java.util.Map<K, V>, V> {
/**
* Return the Java type of the map key
* @return Java key type
*/
Class<K> getKeyJavaType();
/**
* Return the type representing the key type of the map
* @return type representing key type
*/
Type<K> getKeyType();
}Usage Example:
Metamodel metamodel = entityManager.getMetamodel();
EntityType<Customer> customerType = metamodel.entity(Customer.class);
// Access singular attributes
SingularAttribute<Customer, String> nameAttr =
customerType.getSingularAttribute("name", String.class);
boolean isOptional = nameAttr.isOptional();
// Access collection attributes
SetAttribute<Customer, Order> ordersAttr =
customerType.getSet("orders", Order.class);
Type<Order> elementType = ordersAttr.getElementType();
// Iterate all attributes
for (Attribute<? super Customer, ?> attr : customerType.getAttributes()) {
System.out.println("Attribute: " + attr.getName() +
", Type: " + attr.getPersistentAttributeType());
}Annotation for generating type-safe metamodel classes.
/**
* Designates a class as a static metamodel for an entity, mapped superclass, or embeddable class
* @since 2.0
*/
@Target(TYPE)
@Retention(RUNTIME)
public @interface StaticMetamodel {
/**
* Class being modeled by the annotated class
* @return class being modeled
*/
Class<?> value();
}Usage Example:
@Entity
public class Customer {
@Id
private Long id;
private String name;
private String email;
@OneToMany(mappedBy = "customer")
private Set<Order> orders;
}
// Generated static metamodel class
@StaticMetamodel(Customer.class)
public class Customer_ {
public static volatile SingularAttribute<Customer, Long> id;
public static volatile SingularAttribute<Customer, String> name;
public static volatile SingularAttribute<Customer, String> email;
public static volatile SetAttribute<Customer, Order> orders;
}
// Usage in Criteria API
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Customer> query = cb.createQuery(Customer.class);
Root<Customer> customer = query.from(Customer.class);
// Type-safe queries using static metamodel
query.select(customer)
.where(cb.equal(customer.get(Customer_.name), "John"));Install with Tessl CLI
npx tessl i tessl/maven-jakarta-persistence--jakarta-persistence-api