JPA Metamodel API

The JPA Metamodel API provides the ability to examine the persistent object model and retrieve details on managed classes and persistent fields and properties, similarly to the ability that Java reflection provides for general Java types.

The Metamodel Interface

The main interface of the JPA Metamodel API is Metamodeljakarta.persistence.metamodel.Metamodel - JPA Interface Provides access to the metamodel of persistent entities in the persistence unit.. It can be obtained either by the EntityManagerFactoryjakarta.persistence.EntityManagerFactory - JPA Interface Interface used to interact with the persistence unit, and to create new instances of EntityManager.'s getMetamodelEntityManagerFactory.getMetamodel() - JPA Method Return an instance of the Metamodel interface for access to the metamodel of the persistence unit. method or by the EntityManagerjakarta.persistence.EntityManager - JPA Interface Interface used to interact with the persistence context.'s getMetamodelEntityManager.getMetamodel() - JPA Method Obtain an instance of the Metamodel interface which provides access to metamodel objects describing the managed types belonging to the persistence unit. method (both methods are equivalent).

For example, given an EntityManagerjakarta.persistence.EntityManager - JPA Interface Interface used to interact with the persistence context., em, a Metamodel instance can be obtained by:

  Metamodeljakarta.persistence.metamodel.Metamodel - JPA Interface
 Provides access to the metamodel of persistent entities in the
 persistence unit. metamodel = em.getMetamodelEntityManager.getMetamodel() - JPA Method
 Obtain an instance of the Metamodel interface which
 provides access to metamodel objects describing the managed
 types belonging to the persistence unit.();

The Metamodeljakarta.persistence.metamodel.Metamodel - JPA Interface Provides access to the metamodel of persistent entities in the persistence unit. interface provides several methods for exploring user defined persistable types (which are referred to as managed types) in the persistent object model.

Three methods can be used to retrieve sets of types:

  // Get all the managed classes:
  // (entity classes, embeddable classes, mapped super classes)
  Set<ManagedTypejakarta.persistence.metamodel.ManagedType - JPA Interface
  Instances of the type {@code ManagedType} represent entity, mapped
  superclass, and embeddable types.> allManagedTypes = metamodel.getManagedTypesMetamodel.getManagedTypes() - JPA Method
 Return the metamodel managed types.();

  // Get all the entity classes:
  Set<EntityTypejakarta.persistence.metamodel.EntityType - JPA Interface
 An instance of {@code EntityType} represents
 an {@linkplain jakarta.persistence.Entity entity}
 type.> allEntityTypes = metamodel.getEntitiesMetamodel.getEntities() - JPA Method
 Return the metamodel entity types.();

  // Get all the embeddable classes:
  Set<EmbeddableTypejakarta.persistence.metamodel.EmbeddableType - JPA Interface
 An instance of {@code EmbeddableType} represents an
 {@linkplain jakarta.persistence.Embeddable embeddable}
 type.> allEmbeddableTypes = metamodel.getEmbeddablesMetamodel.getEmbeddables() - JPA Method
 Return the metamodel embeddable types.();

If managed classes are not listed in the persistence unit (which is optional when using ObjectDB) then only known managed types are returned. This includes all the types whose instances are already stored in the database.

Three additional methods can be used to retrieve a specific type by its Class instance:

  // Get a managed type (entity, embeddable or mapped super classes):
  ManagedTypejakarta.persistence.metamodel.ManagedType - JPA Interface
  Instances of the type {@code ManagedType} represent entity, mapped
  superclass, and embeddable types.<MyClass> type1 = metamodel.managedTypeMetamodel.managedType(cls) - JPA Method
 Return the metamodel managed type representing the
 entity, mapped superclass, or embeddable class.(MyClass.class);

  // Get an entity type:
  EntityTypejakarta.persistence.metamodel.EntityType - JPA Interface
 An instance of {@code EntityType} represents
 an {@linkplain jakarta.persistence.Entity entity}
 type.<MyEntity> type2 = metamodel.entityMetamodel.entity(cls) - JPA Method
 Return the metamodel entity type representing the entity.(MyEntity.class);

  // Get an embeddable type:
  EmbeddableTypejakarta.persistence.metamodel.EmbeddableType - JPA Interface
 An instance of {@code EmbeddableType} represents an
 {@linkplain jakarta.persistence.Embeddable embeddable}
 type.<MyEmbeddableType> type3 =
      metamodel.embeddableMetamodel.embeddable(cls) - JPA Method
 Return the metamodel embeddable type representing the
 embeddable class.(MyEmbeddableType.class);

These three methods can also be used with types that are still unknown to ObjectDB (not listed in the persistence unit and have not been used yet). In this case, calling the method introduces the specified type to ObjectDB.

Type Interface Hierarchy

Types are represented in the Metamodel API by descendant interfaces of the Typejakarta.persistence.metamodel.Type - JPA Interface An instance of the type {@code Type} represents a persistent object or attribute type. interface:

The Typejakarta.persistence.metamodel.Type - JPA Interface An instance of the type {@code Type} represents a persistent object or attribute type. interface provides a thin wrapper of Class with only two methods:

  // Get the underlying Java representation of the type:
  Class cls = type.getJavaTypeType.getJavaType() - JPA Method
 Return the represented Java type.();

  // Get one of BASIC, EMBEDDABLE, ENTITY, MAPPED_SUPERCLASS:
  PersistenceType kind = type.getPersistenceTypeType.getPersistenceType() - JPA Method
 Return the persistence type.();

The ManagedTypejakarta.persistence.metamodel.ManagedType - JPA Interface Instances of the type {@code ManagedType} represent entity, mapped superclass, and embeddable types. interface adds methods for exploring managed fields and properties (which are referred to as attributes). For example:

  // Get all the attributes - including inherited:
  Set<Attributejakarta.persistence.metamodel.Attribute - JPA Interface
 Represents an attribute of a Java type.> attributes1 = managedType.getAttributesManagedType.getAttributes() - JPA Method
 Return the attributes of the managed type.();

  // Get all the attributes - excluding inherited:
  Set<Attributejakarta.persistence.metamodel.Attribute - JPA Interface
 Represents an attribute of a Java type.> attributes2 = managedType.getDeclaredAttributesManagedType.getDeclaredAttributes() - JPA Method
 Return the attributes declared by the managed type.();

  // Get a specific attribute - including inherited:
  Attributejakarta.persistence.metamodel.Attribute - JPA Interface
 Represents an attribute of a Java type.<MyClass,String> strAttr1 = managedType.getAttributeManagedType.getAttribute(name) - JPA Method
 Return the attribute of the managed
 type that corresponds to the specified name.("name");

  // Get a specific attribute - excluding inherited:
  Attributejakarta.persistence.metamodel.Attribute - JPA Interface
 Represents an attribute of a Java type.<MyClass,String> strAttr2 =
      managedType.getDeclaredAttributeManagedType.getDeclaredAttribute(name) - JPA Method
 Return the attribute declared by the managed
 type that corresponds to the specified name.("name");

Additional methods are defined in ManagedTypejakarta.persistence.metamodel.ManagedType - JPA Interface Instances of the type {@code ManagedType} represent entity, mapped superclass, and embeddable types. to return attributes of Collection, List, Set a Map types in a type safe manner.

The IdentifiableTypejakarta.persistence.metamodel.IdentifiableType - JPA Interface An instance of the type {@code IdentifiableType} represents an entity or mapped superclass type. adds methods for retrieving information on the primary key and the version attributes and the super type. For example:

  // Get the super type:
  IdentifiableTypejakarta.persistence.metamodel.IdentifiableType - JPA Interface
 An instance of the type {@code IdentifiableType} represents an
 entity or mapped superclass type.<MyEntity> superType = entityType.getSupertypeIdentifiableType.getSupertype() - JPA Method
 Return the identifiable type that corresponds to the most
 specific mapped superclass or entity extended by the entity
 or mapped superclass.();

  // Checks if the type has a single ID attribute:
  boolean hasSingleId = entityType.hasSingleIdAttributeIdentifiableType.hasSingleIdAttribute() - JPA Method
 Whether the identifiable type has a single id attribute.();

  // Gets a single ID attribute - including inherited:
  SingularAttributejakarta.persistence.metamodel.SingularAttribute - JPA Interface
 Instances of the type {@code SingularAttribute} represents persistent
 single-valued properties or fields.<MyEntity,Long> id1 = entityType.getIdIdentifiableType.getId(type) - JPA Method
 Return the attribute that corresponds to the id attribute of
 the entity or mapped superclass.(Long.class);

  // Gets a single ID attribute - excluding inherited:
  SingularAttributejakarta.persistence.metamodel.SingularAttribute - JPA Interface
 Instances of the type {@code SingularAttribute} represents persistent
 single-valued properties or fields.<MyEntity,Long> id2 =
      entityType.getDeclaredIdIdentifiableType.getDeclaredId(type) - JPA Method
 Return the attribute that corresponds to the id attribute
 declared by the entity or mapped superclass.(Long.class);

  // Checks if the type has a version attribute:
  boolean hasVersion = entityType.hasVersionAttributeIdentifiableType.hasVersionAttribute() - JPA Method
 Whether the identifiable type has a version attribute.();

  // Gets the version attribute - excluding inherited:
  SingularAttributejakarta.persistence.metamodel.SingularAttribute - JPA Interface
 Instances of the type {@code SingularAttribute} represents persistent
 single-valued properties or fields.<MyEntity,Long> v1 = entityType.getVersionIdentifiableType.getVersion(type) - JPA Method
 Return the attribute that corresponds to the version
 attribute of the entity or mapped superclass.(Long.class);

  // Gets the version attribute - including inherited:
  SingularAttributejakarta.persistence.metamodel.SingularAttribute - JPA Interface
 Instances of the type {@code SingularAttribute} represents persistent
 single-valued properties or fields.<MyEntity,Long> v2 =
      entityType.getDeclaredVersionIdentifiableType.getDeclaredVersion(type) - JPA Method
 Return the attribute that corresponds to the version
 attribute declared by the entity or mapped superclass.(Long.class);

Additional methods are defined in IdentifiableTypejakarta.persistence.metamodel.IdentifiableType - JPA Interface An instance of the type {@code IdentifiableType} represents an entity or mapped superclass type. to support an ID class when using multiple ID fields or properties.

Finally, the EntityTypejakarta.persistence.metamodel.EntityType - JPA Interface An instance of {@code EntityType} represents an {@linkplain jakarta.persistence.Entity entity} type. interface adds only one additional method for getting the entity name:

  String entityName = entityType.getNameEntityType.getName() - JPA Method
 Return the entity name.();

Attribute Interface Hierarchy

Managed fields and properties are represented by the Attributejakarta.persistence.metamodel.Attribute - JPA Interface Represents an attribute of a Java type. interface and its descendant interfaces:

The Attributejakarta.persistence.metamodel.Attribute - JPA Interface Represents an attribute of a Java type. interface provides methods for retrieving field and property details. For example:

  // Get the field (or property) name:
  String name = attr.getNameAttribute.getName() - JPA Method
 Return the name of the attribute.();

  // Get Java representation of the field (or property) type:
  Class<Integer> attr.getJavaTypeAttribute.getJavaType() - JPA Method
 Return the Java type of the represented attribute.();

  // Get Java reflection representation of the field (or property) type:
  Member member = attr.getJavaMemberAttribute.getJavaMember() - JPA Method
 Return the java.lang.reflect.Member for the
  represented attribute.();

  // Get the type in which this field (or property) is defined:
  ManagedTypejakarta.persistence.metamodel.ManagedType - JPA Interface
  Instances of the type {@code ManagedType} represent entity, mapped
  superclass, and embeddable types.<MyEntity> entityType = attr.getDeclaringTypeAttribute.getDeclaringType() - JPA Method
 Return the managed type representing the type in which
 the attribute was declared.();

Few other methods are defined in Attributejakarta.persistence.metamodel.Attribute - JPA Interface Represents an attribute of a Java type. and in MapAttributejakarta.persistence.metamodel.MapAttribute - JPA Interface Instances of the type {@code MapAttribute} represent persistent java.util.Map-valued attributes. to support additional details.