JPA Metamodel API
The JPA Metamodel API enables you to examine the persistent object model and retrieve details about managed classes, persistent fields, and properties, similar to how Java reflection provides this capability for general Java types.
This page covers the following topics:
The Metamodel interfaceType interface hierarchyAttribute interface hierarchyThe 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.. You can obtain an instance from either the EntityManagerFactoryjakarta.persistence.EntityManagerFactoryInterface used to interact with the persistence unit, and to create new instances of EntityManager .'s getMetamodel()jakarta.persistence.EntityManagerFactory.getMetamodel()Return an instance of the Metamodel interface for access to the metamodel of the persistence unit. method or the EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context.'s getMetamodel()jakarta.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. instance named em, you can get a Metamodel instance as follows:
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 (also known as managed types) in the persistent object model.
Three methods 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 (an optional step when using ObjectDB), these methods return only known managed types. This includes all types whose instances are already stored in the database.
Three additional methods 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);
You can also use these three methods with types that are not yet known to ObjectDB (for example, types that are not listed in the persistence unit and have not been persisted). In this case, calling one of these methods introduces the specified type to ObjectDB.
Type interface hierarchy
In the Metamodel API, types are represented by interfaces that extend the Typejakarta.persistence.metamodel.TypeAn instance of the type Type represents a persistent object or attribute type. interface:
BasicTypejakarta.persistence.metamodel.BasicTypeAn instance of BasicType represents a basic type (possibly an enumerated , LOB , or temporal type).: Represents system-defined types.ManagedTypejakarta.persistence.metamodel.ManagedTypeInstances of the type ManagedType represent entity, mapped superclass, and embeddable types.: The superinterface for interfaces that represent user-defined types:EmbeddableTypejakarta.persistence.metamodel.EmbeddableTypeAn instance of EmbeddableType represents an embeddable type.: Represents user-defined embeddable classes.IdentifiableTypejakarta.persistence.metamodel.IdentifiableTypeAn instance of the type IdentifiableType represents an entity or mapped superclass type.: The superinterface for:MappedSuperclassTypejakarta.persistence.metamodel.MappedSuperclassTypeAn instance of the type MappedSuperclassType represents a mapped superclass type.: Represents user-defined mapped superclasses.EntityTypejakarta.persistence.metamodel.EntityTypeAn instance of EntityType represents an entity type.: Represents user-defined entity classes.
The Typejakarta.persistence.metamodel.TypeAn instance of the type Type represents a persistent object or attribute type. interface is a thin wrapper for Class and provides 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, also known 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");
The ManagedTypejakarta.persistence.metamodel.ManagedTypeInstances of the type ManagedType represent entity, mapped superclass, and embeddable types. interface defines additional methods to return attributes of Collection, List, Set, and Map types in a type-safe manner.
The IdentifiableTypejakarta.persistence.metamodel.IdentifiableTypeAn instance of the type IdentifiableType represents an entity or mapped superclass type. interface adds methods for retrieving information about the primary key, version attributes, and the supertype. 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 you use multiple ID fields or properties.
Finally, the EntityTypejakarta.persistence.metamodel.EntityTypeAn instance of EntityType represents an entity type. interface adds only one method, which gets 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 subinterfaces:
SingularAttributejakarta.persistence.metamodel.SingularAttributeInstances of the type SingularAttribute represents persistent single-valued properties or fields.: Represents single-valued attributes.PluralAttributejakarta.persistence.metamodel.PluralAttributeInstances of the type PluralAttribute represent persistent collection-valued attributes.: The superinterface for interfaces that represent multi-valued attributes:CollectionAttributejakarta.persistence.metamodel.CollectionAttributeInstances of the type CollectionAttribute represent persistent Collection<E> -valued attributes.: Represents attributes ofCollectiontypes.SetAttributejakarta.persistence.metamodel.SetAttributeInstances of the type SetAttribute represent persistent Set<E> -valued attributes.: Represents attributes ofSettypes.ListAttributejakarta.persistence.metamodel.ListAttributeInstances of the type ListAttribute represent persistent List<E> -valued attributes.: Represents attributes ofListtypes.MapAttributejakarta.persistence.metamodel.MapAttributeInstances of the type MapAttribute represent persistent Map<K,V> -valued attributes.: Represents attributes ofMaptypes.
The Attributejakarta.persistence.metamodel.AttributeRepresents an attribute of a Java type. interface provides methods for retrieving details about fields and properties. 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.();
A few other methods are defined in Attributejakarta.persistence.metamodel.AttributeRepresents an attribute of a Java type. and MapAttributejakarta.persistence.metamodel.MapAttributeInstances of the type MapAttribute represent persistent Map<K,V> -valued attributes. to provide additional details.