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.
This page covers the following topics:
The Metamodel Interface
The main interface of the JPA Metamodel API is Metamodel
javax.persistence.metamodel.Metamodel - JPA InterfaceProvides access to the metamodel of persistent entities in the persistence unit.. It can be obtained either by the EntityManagerFactory
javax.persistence.EntityManagerFactory - JPA InterfaceInterface used to interact with the entity manager factory for the persistence unit.'s getMetamodel
EntityManagerFactory.getMetamodel() - JPA MethodReturn an instance of Metamodel
interface for access to the metamodel of the persistence unit. method or by the EntityManager
javax.persistence.EntityManager - JPA InterfaceInterface used to interact with the persistence context.'s getMetamodel
EntityManager.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 EntityManager
javax.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 Metamodel
javax.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 Interface Instances of the typeManagedType
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 Interface Instances of the typeEntityType
represent entity types.> allEntityTypes = metamodel.getEntitiesMetamodel.getEntities() - JPA MethodReturn the metamodel entity types.(); // Get all the embeddable classes: Set<EmbeddableTypejavax.persistence.metamodel.EmbeddableType- JPA Interface Instances of the typeEmbeddableType
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 Interface Instances of the typeManagedType
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 Interface Instances of the typeEntityType
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 Interface Instances of the typeEmbeddableType
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 Type
javax.persistence.metamodel.TypeType
represent persistent object or attribute types. interface:
BasicType
javax.persistence.metamodel.BasicType- JPA Interface Instances of the typeBasicType
represent basic types (including temporal and enumerated types). - represents system defined types.ManagedType
javax.persistence.metamodel.ManagedType- JPA Interface Instances of the typeManagedType
represent entity, mapped superclass, and embeddable types. is an ancestor of interfaces that represent user defined types:EmbeddableType
javax.persistence.metamodel.EmbeddableType- JPA Interface Instances of the typeEmbeddableType
represent embeddable types. - represents user defined embeddable classes.IdentifiableType
javax.persistence.metamodel.IdentifiableType- JPA Interface Instances of the typeIdentifiableType
represent entity or mapped superclass types. is as a super interface of:MappedSuperclassType
javax.persistence.metamodel.MappedSuperclassType- JPA Interface Instances of the typeMappedSuperclassType
represent mapped superclass types. - represents user defined mapped super classes.EntityType
javax.persistence.metamodel.EntityType- JPA Interface Instances of the typeEntityType
represent entity types. - represents user defined entity classes.
The Type
javax.persistence.metamodel.TypeType
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 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 ManagedType
javax.persistence.metamodel.ManagedTypeManagedType
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 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<Attributejavax.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: Attributejavax.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: Attributejavax.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 ManagedType
javax.persistence.metamodel.ManagedTypeManagedType
represent entity, mapped superclass, and embeddable types. to return attributes of Collection
, List
, Set
a Map
types in a type safe manner.
The IdentifiableType
javax.persistence.metamodel.IdentifiableTypeIdentifiableType
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 Interface Instances of the typeIdentifiableType
represent entity or mapped superclass types.<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: SingularAttributejavax.persistence.metamodel.SingularAttribute- JPA Interface Instances of the typeSingularAttribute
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: SingularAttributejavax.persistence.metamodel.SingularAttribute- JPA Interface Instances of the typeSingularAttribute
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: SingularAttributejavax.persistence.metamodel.SingularAttribute- JPA Interface Instances of the typeSingularAttribute
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: SingularAttributejavax.persistence.metamodel.SingularAttribute- JPA Interface Instances of the typeSingularAttribute
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 IdentifiableType
javax.persistence.metamodel.IdentifiableTypeIdentifiableType
represent entity or mapped superclass types. to support an ID class when using multiple ID fields or properties.
Finally, the EntityType
javax.persistence.metamodel.EntityTypeEntityType
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 Attribute
javax.persistence.metamodel.Attribute
SingularAttribute
javax.persistence.metamodel.SingularAttribute- JPA Interface Instances of the typeSingularAttribute
represents persistent single-valued properties or fields. - represents single value attributes.PluralAttribute
javax.persistence.metamodel.PluralAttribute- JPA Interface Instances of the typePluralAttribute
represent persistent collection-valued attributes. is an ancestor of interfaces that represent multi value attributes:CollectionAttribute
javax.persistence.metamodel.CollectionAttribute- JPA Interface Instances of the typeCollectionAttribute
represent persistentjava.util.Collection
-valued attributes. - represents attributes ofCollection
types.SetAttribute
javax.persistence.metamodel.SetAttribute- JPA Interface Instances of the typeSetAttribute
represent persistentjava.util.Set
-valued attributes. - represents attributes ofSet
types.ListAttribute
javax.persistence.metamodel.ListAttribute- JPA Interface Instances of the typeListAttribute
represent persistentjavax.util.List
-valued attributes. represents attributes ofList
types.MapAttribute
javax.persistence.metamodel.MapAttribute- JPA Interface Instances of the typeMapAttribute
represent persistentjava.util.Map
-valued attributes. - represents attributes ofMap
types.
The Attribute
javax.persistence.metamodel.Attribute
// 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 thejava.lang.reflect.Member
for the represented attribute.(); // Get the type in which this field (or property) is defined: ManagedTypejavax.persistence.metamodel.ManagedType- JPA Interface Instances of the typeManagedType
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 Attribute
javax.persistence.metamodel.AttributeMapAttribute
javax.persistence.metamodel.MapAttributeMapAttribute
represent persistent java.util.Map
-valued attributes. to support additional details.