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.MetamodelProvides access to the metamodel of persistent entities in the persistence unit.. It can be obtained either by the EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager.'s getMetamodeljakarta.persistence.EntityManagerFactory.getMetamodel()Return an instance of the Metamodel interface for access to the metamodel of the persistence unit. method or by the EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context.'s getMetamodeljakarta.persistence.EntityManager.getMetamodel()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.EntityManagerInterface used to interact with the persistence context., em, a Metamodel instance can be obtained by:

  Metamodeljakarta.persistence.metamodel.MetamodelProvides access to the metamodel of persistent entities in the persistence unit. metamodel = em.getMetamodeljakarta.persistence.EntityManager.getMetamodel()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.MetamodelProvides 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.ManagedTypeInstances of the type ManagedType represent entity, mapped superclass, and embeddable types.> allManagedTypes = metamodel.getManagedTypesjakarta.persistence.metamodel.Metamodel.getManagedTypes()Return the metamodel managed types.();

  // Get all the entity classes:
  Set<EntityTypejakarta.persistence.metamodel.EntityTypeAn instance of EntityType represents an entity type.> allEntityTypes = metamodel.getEntitiesjakarta.persistence.metamodel.Metamodel.getEntities()Return the metamodel entity types.();

  // Get all the embeddable classes:
  Set<EmbeddableTypejakarta.persistence.metamodel.EmbeddableTypeAn instance of EmbeddableType represents an embeddable type.> allEmbeddableTypes = metamodel.getEmbeddablesjakarta.persistence.metamodel.Metamodel.getEmbeddables()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.ManagedTypeInstances of the type ManagedType represent entity, mapped superclass, and embeddable types.<MyClass> type1 = metamodel.managedTypejakarta.persistence.metamodel.Metamodel.managedType(Class)Return the metamodel managed type representing the entity, mapped superclass, or embeddable class.(MyClass.class);

  // Get an entity type:
  EntityTypejakarta.persistence.metamodel.EntityTypeAn instance of EntityType represents an entity type.<MyEntity> type2 = metamodel.entityjakarta.persistence.metamodel.Metamodel.entity(Class)Return the metamodel entity type representing the entity.(MyEntity.class);

  // Get an embeddable type:
  EmbeddableTypejakarta.persistence.metamodel.EmbeddableTypeAn instance of EmbeddableType represents an embeddable type.<MyEmbeddableType> type3 =
      metamodel.embeddablejakarta.persistence.metamodel.Metamodel.embeddable(Class)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.TypeAn instance of the type Type represents a persistent object or attribute type. interface:

The Typejakarta.persistence.metamodel.TypeAn instance of the type 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.getJavaTypejakarta.persistence.metamodel.Type.getJavaType()Return the represented Java type.();

  // Get one of BASIC, EMBEDDABLE, ENTITY, MAPPED_SUPERCLASS:
  PersistenceType kind = type.getPersistenceTypejakarta.persistence.metamodel.Type.getPersistenceType()Return the persistence type.();

The ManagedTypejakarta.persistence.metamodel.ManagedTypeInstances of the type 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.AttributeRepresents an attribute of a Java type.> attributes1 = managedType.getAttributesjakarta.persistence.metamodel.ManagedType.getAttributes()Return the attributes of the managed type.();

  // Get all the attributes - excluding inherited:
  Set<Attributejakarta.persistence.metamodel.AttributeRepresents an attribute of a Java type.> attributes2 = managedType.getDeclaredAttributesjakarta.persistence.metamodel.ManagedType.getDeclaredAttributes()Return the attributes declared by the managed type.();

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

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

Additional methods are defined in ManagedTypejakarta.persistence.metamodel.ManagedTypeInstances of the type 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.IdentifiableTypeAn instance of the type 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.IdentifiableTypeAn instance of the type IdentifiableType represents an entity or mapped superclass type.<MyEntity> superType = entityType.getSupertypejakarta.persistence.metamodel.IdentifiableType.getSupertype()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.hasSingleIdAttributejakarta.persistence.metamodel.IdentifiableType.hasSingleIdAttribute()Whether the identifiable type has a single id attribute.();

  // Gets a single ID attribute - including inherited:
  SingularAttributejakarta.persistence.metamodel.SingularAttributeInstances of the type SingularAttribute represents persistent single-valued properties or fields.<MyEntity,Long> id1 = entityType.getIdjakarta.persistence.metamodel.IdentifiableType.getId(Class)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.SingularAttributeInstances of the type SingularAttribute represents persistent single-valued properties or fields.<MyEntity,Long> id2 =
      entityType.getDeclaredIdjakarta.persistence.metamodel.IdentifiableType.getDeclaredId(Class)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.hasVersionAttributejakarta.persistence.metamodel.IdentifiableType.hasVersionAttribute()Whether the identifiable type has a version attribute.();

  // Gets the version attribute - excluding inherited:
  SingularAttributejakarta.persistence.metamodel.SingularAttributeInstances of the type SingularAttribute represents persistent single-valued properties or fields.<MyEntity,Long> v1 = entityType.getVersionjakarta.persistence.metamodel.IdentifiableType.getVersion(Class)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.SingularAttributeInstances of the type SingularAttribute represents persistent single-valued properties or fields.<MyEntity,Long> v2 =
      entityType.getDeclaredVersionjakarta.persistence.metamodel.IdentifiableType.getDeclaredVersion(Class)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.IdentifiableTypeAn instance of the type 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.EntityTypeAn instance of EntityType represents an entity type. interface adds only one additional method for getting the entity name:

  String entityName = entityType.getNamejakarta.persistence.metamodel.EntityType.getName()Return the entity name.();

Attribute Interface Hierarchy

Managed fields and properties are represented by the Attributejakarta.persistence.metamodel.AttributeRepresents an attribute of a Java type. interface and its descendant interfaces:

The Attributejakarta.persistence.metamodel.AttributeRepresents 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.getNamejakarta.persistence.metamodel.Attribute.getName()Return the name of the attribute.();

  // Get Java representation of the field (or property) type:
  Class<Integer> attr.getJavaTypejakarta.persistence.metamodel.Attribute.getJavaType()Return the Java type of the represented attribute.();

  // Get Java reflection representation of the field (or property) type:
  Member member = attr.getJavaMemberjakarta.persistence.metamodel.Attribute.getJavaMember()Return the Member for the represented attribute.();

  // Get the type in which this field (or property) is defined:
  ManagedTypejakarta.persistence.metamodel.ManagedTypeInstances of the type ManagedType represent entity, mapped superclass, and embeddable types.<MyEntity> entityType = attr.getDeclaringTypejakarta.persistence.metamodel.Attribute.getDeclaringType()Return the managed type representing the type in which the attribute was declared.();

Few other methods are defined in Attributejakarta.persistence.metamodel.AttributeRepresents an attribute of a Java type. and in MapAttributejakarta.persistence.metamodel.MapAttributeInstances of the type MapAttribute represent persistent Map<K,V>-valued attributes. to support additional details.