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 Metamodeljavax.persistence.metamodel.Metamodel - JPA InterfaceProvides access to the metamodel of persistent entities in the persistence unit.. It can be obtained either by the EntityManagerFactoryjavax.persistence.EntityManagerFactory - JPA InterfaceInterface used to interact with the entity manager factory for the persistence unit.'s getMetamodelEntityManagerFactory.getMetamodel() - JPA MethodReturn an instance of Metamodel interface for access to the metamodel of the persistence unit. method or by the EntityManagerjavax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context.'s getMetamodelEntityManager.getMetamodel() - JPA MethodReturn an instance of Metamodel interface for access to the metamodel of the persistence unit. method (both methods are equivalent).

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

  Metamodeljavax.persistence.metamodel.Metamodel - JPA InterfaceProvides access to the metamodel of persistent
 entities in the persistence unit. metamodel = em.getMetamodelEntityManager.getMetamodel() - JPA MethodReturn an instance of Metamodel interface for access to the
 metamodel of the persistence unit.();

The Metamodeljavax.persistence.metamodel.Metamodel - JPA InterfaceProvides 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<ManagedTypejavax.persistence.metamodel.ManagedType - JPA InterfaceInstances of the type ManagedType represent entity, mapped
  superclass, and embeddable types.> allManagedTypes = metamodel.getManagedTypesMetamodel.getManagedTypes() - JPA MethodReturn the metamodel managed types.();

  // Get all the entity classes:
  Set<EntityTypejavax.persistence.metamodel.EntityType - JPA InterfaceInstances of the type EntityType represent entity types.> allEntityTypes = metamodel.getEntitiesMetamodel.getEntities() - JPA MethodReturn the metamodel entity types.();

  // Get all the embeddable classes:
  Set<EmbeddableTypejavax.persistence.metamodel.EmbeddableType - JPA InterfaceInstances of the type EmbeddableType represent embeddable types.> allEmbeddableTypes = metamodel.getEmbeddablesMetamodel.getEmbeddables() - JPA MethodReturn 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):
  ManagedTypejavax.persistence.metamodel.ManagedType - JPA InterfaceInstances of the type ManagedType represent entity, mapped
  superclass, and embeddable types.<MyClass> type1 = metamodel.managedTypeMetamodel.managedType(cls) - JPA MethodReturn the metamodel managed type representing the
  entity, mapped superclass, or embeddable class.(MyClass.class);

  // Get an entity type:
  EntityTypejavax.persistence.metamodel.EntityType - JPA InterfaceInstances of the type EntityType represent entity types.<MyEntity> type2 = metamodel.entityMetamodel.entity(cls) - JPA MethodReturn the metamodel entity type representing the entity.(MyEntity.class);

  // Get an embeddable type:
  EmbeddableTypejavax.persistence.metamodel.EmbeddableType - JPA InterfaceInstances of the type EmbeddableType represent embeddable types.<MyEmbeddableType> type3 =
      metamodel.embeddableMetamodel.embeddable(cls) - JPA MethodReturn 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 Typejavax.persistence.metamodel.Type - JPA InterfaceInstances of the type Type represent persistent object or attribute types. interface:

The Typejavax.persistence.metamodel.Type - JPA InterfaceInstances of the type Type represent persistent object or attribute types. 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 MethodReturn the represented Java type.();

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

The ManagedTypejavax.persistence.metamodel.ManagedType - JPA InterfaceInstances 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<Attributejavax.persistence.metamodel.Attribute - JPA InterfaceRepresents an attribute of a Java type.> attributes1 = managedType.getAttributesManagedType.getAttributes() - JPA MethodReturn the attributes of the managed type.();

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

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

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

Additional methods are defined in ManagedTypejavax.persistence.metamodel.ManagedType - JPA InterfaceInstances 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 IdentifiableTypejavax.persistence.metamodel.IdentifiableType - JPA InterfaceInstances of the type IdentifiableType represent entity or mapped superclass types. adds methods for retrieving information on the primary key and the version attributes and the super type. For example:

  // Get the super type:
  IdentifiableTypejavax.persistence.metamodel.IdentifiableType - JPA InterfaceInstances of the type IdentifiableType represent entity or
  mapped superclass types.<MyEntity> superType = entityType.getSupertypeIdentifiableType.getSupertype() - JPA MethodReturn 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 MethodWhether the identifiable type has a single id attribute.();

  // Gets a single ID attribute - including inherited:
  SingularAttributejavax.persistence.metamodel.SingularAttribute - JPA InterfaceInstances of the type SingularAttribute represents persistent
 single-valued properties or fields.<MyEntity,Long> id1 = entityType.getIdIdentifiableType.getId(type) - JPA MethodReturn the attribute that corresponds to the id attribute of
  the entity or mapped superclass.(Long.class);

  // Gets a single ID attribute - excluding inherited:
  SingularAttributejavax.persistence.metamodel.SingularAttribute - JPA InterfaceInstances of the type SingularAttribute represents persistent
 single-valued properties or fields.<MyEntity,Long> id2 =
      entityType.getDeclaredIdIdentifiableType.getDeclaredId(type) - JPA MethodReturn 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 MethodWhether the identifiable type has a version attribute.();

  // Gets the version attribute - excluding inherited:
  SingularAttributejavax.persistence.metamodel.SingularAttribute - JPA InterfaceInstances of the type SingularAttribute represents persistent
 single-valued properties or fields.<MyEntity,Long> v1 = entityType.getVersionIdentifiableType.getVersion(type) - JPA MethodReturn the attribute that corresponds to the version
  attribute of the entity or mapped superclass.(Long.class);

  // Gets the version attribute - including inherited:
  SingularAttributejavax.persistence.metamodel.SingularAttribute - JPA InterfaceInstances of the type SingularAttribute represents persistent
 single-valued properties or fields.<MyEntity,Long> v2 =
      entityType.getDeclaredVersionIdentifiableType.getDeclaredVersion(type) - JPA MethodReturn the attribute that corresponds to the version
  attribute declared by the entity or mapped superclass.(Long.class);

Additional methods are defined in IdentifiableTypejavax.persistence.metamodel.IdentifiableType - JPA InterfaceInstances of the type IdentifiableType represent entity or mapped superclass types. to support an ID class when using multiple ID fields or properties.

Finally, the EntityTypejavax.persistence.metamodel.EntityType - JPA InterfaceInstances of the type EntityType represent entity types. interface adds only one additional method for getting the entity name:

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

Attribute Interface Hierarchy

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

The Attributejavax.persistence.metamodel.Attribute - JPA InterfaceRepresents 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 MethodReturn the name of the attribute.();

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

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

  // Get the type in which this field (or property) is defined:
  ManagedTypejavax.persistence.metamodel.ManagedType - JPA InterfaceInstances of the type ManagedType represent entity, mapped
  superclass, and embeddable types.<MyEntity> entityType = attr.getDeclaringTypeAttribute.getDeclaringType() - JPA MethodReturn the managed type representing the type in which
  the attribute was declared.();

Few other methods are defined in Attributejavax.persistence.metamodel.Attribute - JPA InterfaceRepresents an attribute of a Java type. and in MapAttributejavax.persistence.metamodel.MapAttribute - JPA InterfaceInstances of the type MapAttribute represent persistent java.util.Map-valued attributes. to support additional details.