752 words

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.MetamodelJPA interfaceProvides access to the metamodel of persistent entities in the persistence unit.
See JavaDoc Reference Page...
. It can be obtained either by the EntityManagerFactoryjavax.persistence.EntityManagerFactoryJPA interfaceInterface used to interact with the entity manager factory for the persistence unit.
See JavaDoc Reference Page...
's getMetamodelgetMetamodel()EntityManagerFactory's methodReturn an instance of Metamodel interface for access to the metamodel of the persistence unit.
See JavaDoc Reference Page...
method or by the EntityManagerjavax.persistence.EntityManagerJPA interfaceInterface used to interact with the persistence context.
See JavaDoc Reference Page...
's getMetamodelgetMetamodel()EntityManager's methodReturn an instance of Metamodel interface for access to the metamodel of the persistence unit.
See JavaDoc Reference Page...
method (both methods are equivalent).

For example, given an EntityManagerjavax.persistence.EntityManagerJPA interfaceInterface used to interact with the persistence context.
See JavaDoc Reference Page...
, em, a Metamodel instance can be obtained by:

The Metamodeljavax.persistence.metamodel.MetamodelJPA interfaceProvides access to the metamodel of persistent entities in the persistence unit.
See JavaDoc Reference Page...
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:

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:

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.TypeJPA interfaceInstances of the type Type represent persistent object or attribute types.
See JavaDoc Reference Page...
interface:

The Typejavax.persistence.metamodel.TypeJPA interfaceInstances of the type Type represent persistent object or attribute types.
See JavaDoc Reference Page...
interfaces provides a thin wrapper of Class with only two methods:

  // Get the underlying Java representation of the type:
  Class cls = type.getJavaTypegetJavaType()Type's methodReturn the represented Java type.
See JavaDoc Reference Page...
();   // Get one of BASIC, EMBEDDABLE, ENTITY, MAPPED_SUPERCLASS: PersistenceType kind = type.getPersistenceTypegetPersistenceType()Type's methodReturn the persistence type.
See JavaDoc Reference Page...
();

The ManagedTypejavax.persistence.metamodel.ManagedTypeJPA interfaceInstances of the type ManagedType represent entity, mapped superclass, and embeddable types.
See JavaDoc Reference Page...
 interface adds methods for exploring managed fields and properties (which are referred to as attributes). For example:

Additional methods are defined in ManagedTypejavax.persistence.metamodel.ManagedTypeJPA interfaceInstances of the type ManagedType represent entity, mapped superclass, and embeddable types.
See JavaDoc Reference Page...
 to return attributes of Collection, List, Set a Map types in a type safe manner.

The IdentifiableTypejavax.persistence.metamodel.IdentifiableTypeJPA interfaceInstances of the type IdentifiableType represent entity or mapped superclass types.
See JavaDoc Reference Page...
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.IdentifiableTypeJPA interfaceInstances of the type IdentifiableType represent entity or mapped superclass types.
See JavaDoc Reference Page...
<MyEntity> superType = entityType.getSupertypegetSupertype()IdentifiableType's methodReturn the identifiable type that corresponds to the most specific mapped superclass or entity extended by the entity or mapped superclass.
See JavaDoc Reference Page...
();   // Checks if the type has a single ID attribute: boolean hasSingleId = entityType.hasSingleIdAttributehasSingleIdAttribute()IdentifiableType's methodWhether the identifiable type has a single id attribute.
See JavaDoc Reference Page...
();   // Gets a single ID attribute - including inherited: SingularAttributejavax.persistence.metamodel.SingularAttributeJPA interfaceInstances of the type SingularAttribute represents persistent single-valued properties or fields.
See JavaDoc Reference Page...
<MyEntity,Long> id1 = entityType.getIdgetId(type)IdentifiableType's methodReturn the attribute that corresponds to the id attribute of the entity or mapped superclass.
See JavaDoc Reference Page...
(Long.class);   // Gets a single ID attribute - excluding inherited: SingularAttributejavax.persistence.metamodel.SingularAttributeJPA interfaceInstances of the type SingularAttribute represents persistent single-valued properties or fields.
See JavaDoc Reference Page...
<MyEntity,Long> id2 = entityType.getDeclaredIdgetDeclaredId(type)IdentifiableType's methodReturn the attribute that corresponds to the id attribute declared by the entity or mapped superclass.
See JavaDoc Reference Page...
(Long.class);   // Checks if the type has a version attribute: boolean hasVersion = entityType.hasVersionAttributehasVersionAttribute()IdentifiableType's methodWhether the identifiable type has a version attribute.
See JavaDoc Reference Page...
();   // Gets the version attribute - excluding inherited: SingularAttributejavax.persistence.metamodel.SingularAttributeJPA interfaceInstances of the type SingularAttribute represents persistent single-valued properties or fields.
See JavaDoc Reference Page...
<MyEntity,Long> v1 = entityType.getVersiongetVersion(type)IdentifiableType's methodReturn the attribute that corresponds to the version attribute of the entity or mapped superclass.
See JavaDoc Reference Page...
(Long.class);   // Gets the version attribute - including inherited: SingularAttributejavax.persistence.metamodel.SingularAttributeJPA interfaceInstances of the type SingularAttribute represents persistent single-valued properties or fields.
See JavaDoc Reference Page...
<MyEntity,Long> v2 = entityType.getDeclaredVersiongetDeclaredVersion(type)IdentifiableType's methodReturn the attribute that corresponds to the version attribute declared by the entity or mapped superclass.
See JavaDoc Reference Page...
(Long.class);

Additional methods are defined in IdentifiableTypejavax.persistence.metamodel.IdentifiableTypeJPA interfaceInstances of the type IdentifiableType represent entity or mapped superclass types.
See JavaDoc Reference Page...
 to support an ID class when using multiple ID fields or properties.

Finally, the EntityTypejavax.persistence.metamodel.EntityTypeJPA interfaceInstances of the type EntityType represent entity types.
See JavaDoc Reference Page...
interface adds only one additional method for getting the entity name:

Attribute Interface Hierarchy

Managed fields and properties are represented by the Attributejavax.persistence.metamodel.AttributeJPA interfaceRepresents an attribute of a Java type.
See JavaDoc Reference Page...
interfaces and its descendant interfaces:

The Attributejavax.persistence.metamodel.AttributeJPA interfaceRepresents an attribute of a Java type.
See JavaDoc Reference Page...
interface provides methods for retrieving field and property details. For example:

Few other methods are defined in Attributejavax.persistence.metamodel.AttributeJPA interfaceRepresents an attribute of a Java type.
See JavaDoc Reference Page...
 and in MapAttributejavax.persistence.metamodel.MapAttributeJPA interfaceInstances of the type MapAttribute represent persistent java.util.Map-valued attributes.
See JavaDoc Reference Page...
to support additional details.


This documentation explains how to use JPA in the context of the ObjectDB Object Database but mostly relevant
also for ORM JPA implementations, such as Hibernate (and HQL), EclipseLink, TopLink, OpenJPA and DataNucleus.
ObjectDB is not an ORM JPA implementation but an Object Database (ODBMS) for Java with built in JPA 2 support.