Top Description Fields Constructors Methods
org.springframework.orm.jpa

public abstract Class EntityManagerFactoryAccessor

extends Object
implements BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware
Known Direct Subclasses
org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor, org.springframework.orm.jpa.support.SharedEntityManagerBean
Imports
java.util.HashMap, .Map, .Properties, jakarta.persistence.EntityManager, .EntityManagerFactory, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .ListableBeanFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils

Base class for any class that needs to access a JPA EntityManagerFactory, usually in order to obtain a JPA EntityManager. Defines common properties.
Author
Juergen Hoeller
Since
2.0
See Also
EntityManagerFactoryUtils

Field Summary

Modifier and TypeField and Description
private EntityManagerFactory
private final Map<String, Object>
protected final Log
logger

Logger available to subclasses.

private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected EntityManager

Returns:

a new EntityManager
createEntityManager
()

Obtain a new EntityManager from this accessor's EntityManagerFactory.

public EntityManagerFactory
getEntityManagerFactory()

Return the JPA EntityManagerFactory that should be used to create EntityManagers.

public Map<String, Object>
getJpaPropertyMap()

Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.

public String
getPersistenceUnitName()

Return the name of the persistence unit to access the EntityManagerFactory for, if any.

protected EntityManager

Returns:

the transactional EntityManager, or null if none
getTransactionalEntityManager
()

Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any.

protected final EntityManagerFactory

Returns:

the EntityManagerFactory (never null)
obtainEntityManagerFactory
()

Obtain the EntityManagerFactory for actual use.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.
public void
setEntityManagerFactory(EntityManagerFactory emf)

Set the JPA EntityManagerFactory that should be used to create EntityManagers.

public void
setJpaProperties(Properties jpaProperties)

Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

public void
setJpaPropertyMap(Map<String, Object> jpaProperties)

Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

public void
setPersistenceUnitName(String persistenceUnitName)

Set the name of the persistence unit to access the EntityManagerFactory for.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

entityManagerFactoryback to summary
private EntityManagerFactory entityManagerFactory
Annotations
@Nullable
jpaPropertyMapback to summary
private final Map<String, Object> jpaPropertyMap
loggerback to summary
protected final Log logger

Logger available to subclasses.

persistenceUnitNameback to summary
private String persistenceUnitName
Annotations
@Nullable

Constructor Detail

EntityManagerFactoryAccessorback to summary
public EntityManagerFactoryAccessor()

Method Detail

createEntityManagerback to summary
protected EntityManager createEntityManager() throws IllegalStateException

Obtain a new EntityManager from this accessor's EntityManagerFactory.

Can be overridden in subclasses to create specific EntityManager variants.

Returns:EntityManager

a new EntityManager

Exceptions
IllegalStateException:
if this accessor is not configured with an EntityManagerFactory
See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(), jakarta.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
getEntityManagerFactoryback to summary
public EntityManagerFactory getEntityManagerFactory()

Return the JPA EntityManagerFactory that should be used to create EntityManagers.

Annotations
@Nullable
getJpaPropertyMapback to summary
public Map<String, Object> getJpaPropertyMap()

Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.

Useful for specifying entries directly, for example via jpaPropertyMap[myKey].

getPersistenceUnitNameback to summary
public String getPersistenceUnitName()

Return the name of the persistence unit to access the EntityManagerFactory for, if any.

Annotations
@Nullable
getTransactionalEntityManagerback to summary
protected EntityManager getTransactionalEntityManager() throws IllegalStateException

Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any.

Returns:EntityManager

the transactional EntityManager, or null if none

Annotations
@Nullable
Exceptions
IllegalStateException:
if this accessor is not configured with an EntityManagerFactory
See Also
EntityManagerFactoryUtils#getTransactionalEntityManager(jakarta.persistence.EntityManagerFactory), EntityManagerFactoryUtils#getTransactionalEntityManager(jakarta.persistence.EntityManagerFactory, java.util.Map)
obtainEntityManagerFactoryback to summary
protected final EntityManagerFactory obtainEntityManagerFactory()

Obtain the EntityManagerFactory for actual use.

Returns:EntityManagerFactory

the EntityManagerFactory (never null)

Exceptions
IllegalStateException:
in case of no EntityManagerFactory set
Since
5.0
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory) throws BeansException

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly. Falls back to a default EntityManagerFactory bean if no persistence unit specified.

Parameters
beanFactory:BeanFactory

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
Exceptions
BeansException:

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

in case of initialization errors

See Also
setPersistenceUnitName
setEntityManagerFactoryback to summary
public void setEntityManagerFactory(EntityManagerFactory emf)

Set the JPA EntityManagerFactory that should be used to create EntityManagers.

See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(), jakarta.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
setJpaPropertiesback to summary
public void setJpaProperties(Properties jpaProperties)

Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.

See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
setJpaPropertyMapback to summary
public void setJpaPropertyMap(Map<String, Object> jpaProperties)

Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

Can be populated with a "map" or "props" element in XML bean definitions.

See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
setPersistenceUnitNameback to summary
public void setPersistenceUnitName(String persistenceUnitName)

Set the name of the persistence unit to access the EntityManagerFactory for.

This is an alternative to specifying the EntityManagerFactory by direct reference, resolving it by its persistence unit name instead. If no EntityManagerFactory and no persistence unit name have been specified, a default EntityManagerFactory will be retrieved through finding a single unique bean of type EntityManagerFactory.

See Also
setEntityManagerFactory