Package org.springframework.orm.jpa

# Class EntityManagerFactoryAccessor

java.lang.Object
org.springframework.orm.jpa.EntityManagerFactoryAccessor

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanFactoryAware`

Direct Known Subclasses:
  `OpenEntityManagerInViewInterceptor`, `SharedEntityManagerBean`

public abstract class EntityManagerFactoryAccessor extends Object implements org.springframework.beans.factory.BeanFactoryAware
Base class for any class that needs to access a JPA `EntityManagerFactory`, usually in order to obtain a JPA `EntityManager`. Defines common properties.

Since:
  2.0
Author:
  Juergen Hoeller
See Also:
  - `EntityManagerFactoryUtils`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final org.apache.commons.logging.Log`
`logger`
Logger available to subclasses.

## Constructor Summary

Constructors
Constructor
Description
`EntityManagerFactoryAccessor()`

## Method Summary

Modifier and Type
Method
Description
`protected EntityManager`
`createEntityManager()`
Obtain a new EntityManager from this accessor's EntityManagerFactory.
`EntityManagerFactory`
`getEntityManagerFactory()`
Return the JPA EntityManagerFactory that should be used to create EntityManagers.
`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.
`String`
`getPersistenceUnitName()`
Return the name of the persistence unit to access the EntityManagerFactory for, if any.
`protected EntityManager`
`getTransactionalEntityManager()`
Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any.
`protected final EntityManagerFactory`
`obtainEntityManagerFactory()`
Obtain the EntityManagerFactory for actual use.
`void`
`setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)`
Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.
`void`
`setEntityManagerFactory(EntityManagerFactory emf)`
Set the JPA EntityManagerFactory that should be used to create EntityManagers.
`void`
`setJpaProperties(Properties jpaProperties)`
Specify JPA properties, to be passed into `EntityManagerFactory.createEntityManager(Map)` (if any).
`void`
`setJpaPropertyMap(Map<String,Object> jpaProperties)`
Specify JPA properties as a Map, to be passed into `EntityManagerFactory.createEntityManager(Map)` (if any).
`void`
`setPersistenceUnitName(String persistenceUnitName)`
Set the name of the persistence unit to access the EntityManagerFactory for.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses.

## Constructor Details

### EntityManagerFactoryAccessor

public EntityManagerFactoryAccessor()

## Method Details

### setEntityManagerFactory

public void setEntityManagerFactory(@Nullable EntityManagerFactory emf)
Set the JPA EntityManagerFactory that should be used to create EntityManagers.

See Also:
  - `EntityManagerFactory.createEntityManager()`
  - `EntityManagerFactory.createEntityManager(java.util.Map)`

### getEntityManagerFactory

@Nullable public EntityManagerFactory getEntityManagerFactory()
Return the JPA EntityManagerFactory that should be used to create EntityManagers.

### obtainEntityManagerFactory

protected final EntityManagerFactory obtainEntityManagerFactory()
Obtain the EntityManagerFactory for actual use.

Returns:
  the EntityManagerFactory (never `null`)
Throws:
  `IllegalStateException` - in case of no EntityManagerFactory set
Since:
  5.0

### setPersistenceUnitName

public void setPersistenceUnitName(@Nullable 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(jakarta.persistence.EntityManagerFactory)`

### getPersistenceUnitName

@Nullable public String getPersistenceUnitName()
Return the name of the persistence unit to access the EntityManagerFactory for, if any.

### setJpaProperties

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:
  - `EntityManagerFactory.createEntityManager(java.util.Map)`

### setJpaPropertyMap

public void setJpaPropertyMap(@Nullable 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:
  - `EntityManagerFactory.createEntityManager(java.util.Map)`

### getJpaPropertyMap

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]`.

### setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly. Falls back to a default EntityManagerFactory bean if no persistence unit specified.

Specified by:
  `setBeanFactory` in interface `org.springframework.beans.factory.BeanFactoryAware`
Throws:
  `org.springframework.beans.BeansException`
See Also:
  - `setPersistenceUnitName(java.lang.String)`

### createEntityManager

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:
  a new EntityManager
Throws:
  `IllegalStateException` - if this accessor is not configured with an EntityManagerFactory
See Also:
  - `EntityManagerFactory.createEntityManager()`
  - `EntityManagerFactory.createEntityManager(java.util.Map)`

### getTransactionalEntityManager

@Nullable protected EntityManager getTransactionalEntityManager() throws IllegalStateException
Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any.

Returns:
  the transactional EntityManager, or `null` if none
Throws:
  `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)`