Package org.datanucleus.api.jpa
Class JPAEntityManagerFactory
- java.lang.Object
-
- org.datanucleus.api.jpa.JPAEntityManagerFactory
-
- All Implemented Interfaces:
Serializable
,AutoCloseable
,javax.persistence.EntityManagerFactory
,javax.persistence.PersistenceUnitUtil
,javax.persistence.PersistenceUtil
,JPAEntityGraphRegistrationListener
public class JPAEntityManagerFactory extends Object implements javax.persistence.EntityManagerFactory, javax.persistence.PersistenceUnitUtil, JPAEntityGraphRegistrationListener, Serializable
EntityManagerFactory implementation. Caches the "persistence-unit" MetaData information when encountered, in JavaSE mode.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static org.datanucleus.util.NucleusLogger
LOGGER
Logger for enhancing.
-
Constructor Summary
Constructors Constructor Description JPAEntityManagerFactory(String unitName, Map overridingProps)
Constructor when working in a JavaSE environment.JPAEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo unitInfo, Map overridingProps)
Constructor when working in a JavaEE environment.JPAEntityManagerFactory(org.datanucleus.metadata.PersistenceUnitMetaData pumd, Map overridingProps)
Convenience constructor to allow for dynamic persistence-unit creation in JavaSE.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> void
addNamedEntityGraph(String graphName, javax.persistence.EntityGraph<T> graph)
void
addNamedQuery(String name, javax.persistence.Query query)
Method to save the specified query under the provided name, so it can be used as a named query.protected void
assertIsClosed()
void
close()
Method to close the factory.javax.persistence.EntityManager
createEntityManager()
Method to create an (application-managed) entity manager.javax.persistence.EntityManager
createEntityManager(Map overridingProps)
Method to create an (application-managed) entity manager with the specified properties.javax.persistence.EntityManager
createEntityManager(javax.persistence.SynchronizationType syncType)
Create a new JTA application-managed EntityManager with the specified synchronization type.javax.persistence.EntityManager
createEntityManager(javax.persistence.SynchronizationType syncType, Map overridingProps)
Create a new JTA application-managed EntityManager with the specified synchronization type and Map of properties.void
deregisterEntityGraph(String graphName)
void
entityGraphRegistered(JPAEntityGraph eg)
javax.persistence.Cache
getCache()
Accessor for the second level cache.javax.persistence.criteria.CriteriaBuilder
getCriteriaBuilder()
String
getDefinedEntityGraphName()
Set<String>
getEntityGraphNames()
<T> List<javax.persistence.EntityGraph<? super T>>
getEntityGraphsByType(Class<T> entityClass)
Object
getIdentifier(Object entity)
javax.persistence.metamodel.Metamodel
getMetamodel()
String
getName()
javax.persistence.EntityGraph
getNamedEntityGraph(String graphName)
org.datanucleus.NucleusContext
getNucleusContext()
javax.persistence.PersistenceUnitUtil
getPersistenceUnitUtil()
Map<String,Object>
getProperties()
Get the properties and associated values that are in effect for the entity manager factory.JPAQueryCache
getQueryCache()
Accessor for the query results cache.org.datanucleus.store.query.cache.QueryDatastoreCompilationCache
getQueryDatastoreCompilationCache()
Accessor for the query datastore compilation cache.org.datanucleus.store.query.cache.QueryCompilationCache
getQueryGenericCompilationCache()
Accessor for the query generic compilation cache.Set<String>
getSupportedProperties()
Get the names of the properties that are supported for use with the entity manager factory.protected org.datanucleus.PersistenceNucleusContext
initialiseNucleusContext(org.datanucleus.metadata.PersistenceUnitMetaData unitMetaData, Map overridingProps, org.datanucleus.plugin.PluginManager pluginMgr)
Method to initialise a PersistenceManagerFactory that will control the persistence.boolean
isContainerManaged()
Accessor for whether the EMF is managed by a container (JavaEE).boolean
isLoaded(Object entity)
boolean
isLoaded(Object entity, String attrName)
boolean
isOpen()
Accessor for whether the factory is openprotected javax.persistence.EntityManager
newEntityManager(org.datanucleus.PersistenceNucleusContext nucleusCtx, javax.persistence.PersistenceContextType contextType, javax.persistence.SynchronizationType syncType)
Creates anEntityManager
.void
registerEntityGraph(JPAEntityGraph eg, String graphName)
<T> T
unwrap(Class<T> cls)
Return an object of the specified type to allow access to the provider-specific API.
-
-
-
Constructor Detail
-
JPAEntityManagerFactory
public JPAEntityManagerFactory(javax.persistence.spi.PersistenceUnitInfo unitInfo, Map overridingProps)
Constructor when working in a JavaEE environment.- Parameters:
unitInfo
- The "persistent-unit" infooverridingProps
- factory properties overriding those in the "persistence-unit"
-
JPAEntityManagerFactory
public JPAEntityManagerFactory(org.datanucleus.metadata.PersistenceUnitMetaData pumd, Map overridingProps)
Convenience constructor to allow for dynamic persistence-unit creation in JavaSE.- Parameters:
pumd
- Persistence unit metadataoverridingProps
- Properties overriding those defined for this unit
-
-
Method Detail
-
getNucleusContext
public org.datanucleus.NucleusContext getNucleusContext()
-
isContainerManaged
public boolean isContainerManaged()
Accessor for whether the EMF is managed by a container (JavaEE).- Returns:
- Whether managed by a container
-
getName
public String getName()
-
close
public void close()
Method to close the factory.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfacejavax.persistence.EntityManagerFactory
-
isOpen
public boolean isOpen()
Accessor for whether the factory is open- Specified by:
isOpen
in interfacejavax.persistence.EntityManagerFactory
- Returns:
- Whether it is open
-
getQueryCache
public JPAQueryCache getQueryCache()
Accessor for the query results cache.- Returns:
- Query results cache
-
getQueryGenericCompilationCache
public org.datanucleus.store.query.cache.QueryCompilationCache getQueryGenericCompilationCache()
Accessor for the query generic compilation cache.- Returns:
- Query generic compilation cache
-
getQueryDatastoreCompilationCache
public org.datanucleus.store.query.cache.QueryDatastoreCompilationCache getQueryDatastoreCompilationCache()
Accessor for the query datastore compilation cache.- Returns:
- Query datastore compilation cache
-
addNamedQuery
public void addNamedQuery(String name, javax.persistence.Query query)
Method to save the specified query under the provided name, so it can be used as a named query. If there is already a named query of this name it is overwritten.- Specified by:
addNamedQuery
in interfacejavax.persistence.EntityManagerFactory
- Parameters:
query
- The query whose definition we save as named
-
createEntityManager
public javax.persistence.EntityManager createEntityManager()
Method to create an (application-managed) entity manager.- Specified by:
createEntityManager
in interfacejavax.persistence.EntityManagerFactory
- Returns:
- The Entity Manager
-
createEntityManager
public javax.persistence.EntityManager createEntityManager(Map overridingProps)
Method to create an (application-managed) entity manager with the specified properties. This creates a new underlying context since each EMF is locked when created to stop config changes.- Specified by:
createEntityManager
in interfacejavax.persistence.EntityManagerFactory
- Parameters:
overridingProps
- Properties to use for this manager- Returns:
- The Entity Manager
-
createEntityManager
public javax.persistence.EntityManager createEntityManager(javax.persistence.SynchronizationType syncType)
Create a new JTA application-managed EntityManager with the specified synchronization type. This method returns a new EntityManager instance each time it is invoked. The isOpen method will return true on the returned instance.- Specified by:
createEntityManager
in interfacejavax.persistence.EntityManagerFactory
- Parameters:
syncType
- how and when the entity manager should be synchronized with the current JTA transaction- Returns:
- entity manager instance
- Throws:
IllegalStateException
- if the entity manager factory has been configured for resource-local entity managers or has been closed- Since:
- JPA2.1
-
createEntityManager
public javax.persistence.EntityManager createEntityManager(javax.persistence.SynchronizationType syncType, Map overridingProps)
Create a new JTA application-managed EntityManager with the specified synchronization type and Map of properties. This method returns a new EntityManager instance each time it is invoked. The isOpen method will return true on the returned instance.- Specified by:
createEntityManager
in interfacejavax.persistence.EntityManagerFactory
- Parameters:
syncType
- how and when the entity manager should be synchronized with the current JTA transactionoverridingProps
- properties for entity manager; may be null- Returns:
- entity manager instance
- Throws:
IllegalStateException
- if the entity manager factory has been configured for resource-local entity managers or has been closed- Since:
- JPA2.1
-
newEntityManager
protected javax.persistence.EntityManager newEntityManager(org.datanucleus.PersistenceNucleusContext nucleusCtx, javax.persistence.PersistenceContextType contextType, javax.persistence.SynchronizationType syncType)
Creates anEntityManager
. Override if you want to return a different type that implements this interface.- Parameters:
nucleusCtx
- Nucleus ContextcontextType
- The persistence context typesyncType
- Synchronization type- Returns:
- The EntityManager
-
initialiseNucleusContext
protected org.datanucleus.PersistenceNucleusContext initialiseNucleusContext(org.datanucleus.metadata.PersistenceUnitMetaData unitMetaData, Map overridingProps, org.datanucleus.plugin.PluginManager pluginMgr)
Method to initialise a PersistenceManagerFactory that will control the persistence. If the unitMetaData is null will simply create a default context without initialising any MetaData etc. If there is a unitMetaData then all metadata for that unit will be loaded/initialised.- Parameters:
unitMetaData
- The "persistence-unit" metadata (if any)overridingProps
- Properties to override all otherspluginMgr
- Plugin Manager- Returns:
- The PersistenceManagerFactory
-
getProperties
public Map<String,Object> getProperties()
Get the properties and associated values that are in effect for the entity manager factory. Changing the contents of the map does not change the configuration in effect.- Specified by:
getProperties
in interfacejavax.persistence.EntityManagerFactory
- Returns:
- properties
-
getSupportedProperties
public Set<String> getSupportedProperties()
Get the names of the properties that are supported for use with the entity manager factory. These correspond to properties that may be passed to the methods of the EntityManagerFactory interface that take a properties argument. These include all standard properties as well as vendor-specific properties supported by the provider. These properties may or may not currently be in effect.- Returns:
- properties and hints
-
assertIsClosed
protected void assertIsClosed()
-
getCache
public javax.persistence.Cache getCache()
Accessor for the second level cache.- Specified by:
getCache
in interfacejavax.persistence.EntityManagerFactory
- Returns:
- Level 2 cache
-
getMetamodel
public javax.persistence.metamodel.Metamodel getMetamodel()
- Specified by:
getMetamodel
in interfacejavax.persistence.EntityManagerFactory
-
getCriteriaBuilder
public javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()
- Specified by:
getCriteriaBuilder
in interfacejavax.persistence.EntityManagerFactory
-
getPersistenceUnitUtil
public javax.persistence.PersistenceUnitUtil getPersistenceUnitUtil()
- Specified by:
getPersistenceUnitUtil
in interfacejavax.persistence.EntityManagerFactory
-
getIdentifier
public Object getIdentifier(Object entity)
- Specified by:
getIdentifier
in interfacejavax.persistence.PersistenceUnitUtil
-
isLoaded
public boolean isLoaded(Object entity, String attrName)
- Specified by:
isLoaded
in interfacejavax.persistence.PersistenceUnitUtil
- Specified by:
isLoaded
in interfacejavax.persistence.PersistenceUtil
-
isLoaded
public boolean isLoaded(Object entity)
- Specified by:
isLoaded
in interfacejavax.persistence.PersistenceUnitUtil
- Specified by:
isLoaded
in interfacejavax.persistence.PersistenceUtil
-
unwrap
public <T> T unwrap(Class<T> cls)
Return an object of the specified type to allow access to the provider-specific API. If the provider's EntityManagerFactory implementation does not support the specified class, the PersistenceException is thrown.- Specified by:
unwrap
in interfacejavax.persistence.EntityManagerFactory
- Parameters:
cls
- the class of the object to be returned. This is normally either the underlying EntityManagerFactory implementation class or an interface that it implements.- Returns:
- an instance of the specified class
- Throws:
javax.persistence.PersistenceException
- if the provider does not support the call.
-
getNamedEntityGraph
public javax.persistence.EntityGraph getNamedEntityGraph(String graphName)
-
getEntityGraphsByType
public <T> List<javax.persistence.EntityGraph<? super T>> getEntityGraphsByType(Class<T> entityClass)
-
addNamedEntityGraph
public <T> void addNamedEntityGraph(String graphName, javax.persistence.EntityGraph<T> graph)
- Specified by:
addNamedEntityGraph
in interfacejavax.persistence.EntityManagerFactory
-
registerEntityGraph
public void registerEntityGraph(JPAEntityGraph eg, String graphName)
-
deregisterEntityGraph
public void deregisterEntityGraph(String graphName)
-
getDefinedEntityGraphName
public String getDefinedEntityGraphName()
-
entityGraphRegistered
public void entityGraphRegistered(JPAEntityGraph eg)
- Specified by:
entityGraphRegistered
in interfaceJPAEntityGraphRegistrationListener
-
-