Class DefaultJpaDialect
- All Implemented Interfaces:
Serializable, PersistenceExceptionTranslator, JpaDialect
- Direct Known Subclasses:
EclipseLinkJpaDialect, HibernateJpaDialect
JpaDialect interface.
Used as default dialect by JpaTransactionManager.
Simply begins a standard JPA transaction in beginTransaction(EntityManager, TransactionDefinition) and
performs standard exception translation through EntityManagerFactoryUtils.
Supports JPA 4.0's FlushModeType.EXPLICIT for read-only transactions,
if available.
- Since:
- 2.0
- Author:
- Juergen Hoeller
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbeginTransaction(jakarta.persistence.EntityManager entityManager, TransactionDefinition definition) This implementation invokes the standard JPATransaction.beginmethod.voidcleanupTransaction(@Nullable Object transactionData) This implementation resets the flush mode if necessary.deriveEntityAgent(jakarta.persistence.EntityManager entityManager, @Nullable Map<?, ?> properties) Derive a newEntityAgentfrom the givenEntityManagerif possible, sharing the transactional context.getJdbcConnection(jakarta.persistence.EntityManager entityManager, boolean readOnly) This implementation always returnsnull, indicating that no JDBC Connection can be provided.prepareFlushMode(jakarta.persistence.EntityManager entityManager, boolean readOnly) Prepare transaction data for a flush mode reset if necessary.prepareTransaction(jakarta.persistence.EntityManager entityManager, boolean readOnly, @Nullable String name) This implementation returns transaction data for a flush mode reset if necessary, callingprepareFlushMode(EntityManager, boolean)accordingly.voidreleaseJdbcConnection(ConnectionHandle conHandle, jakarta.persistence.EntityManager em) This implementation does nothing, assuming that the Connection will implicitly be closed with the EntityManager.This implementation delegates to EntityManagerFactoryUtils.
-
Constructor Details
-
DefaultJpaDialect
public DefaultJpaDialect()
-
-
Method Details
-
beginTransaction
public @Nullable Object beginTransaction(jakarta.persistence.EntityManager entityManager, TransactionDefinition definition) throws jakarta.persistence.PersistenceException, SQLException, TransactionException This implementation invokes the standard JPATransaction.beginmethod. Throws an InvalidIsolationLevelException if a non-default isolation level is set.This implementation returns transaction data for a flush mode reset if necessary, calling
prepareFlushMode(EntityManager, boolean)accordingly. Can be reused in subclasses or alternatively replaced with custom flush mode handling.- Specified by:
beginTransactionin interfaceJpaDialect- Parameters:
entityManager- the EntityManager to begin a JPA transaction ondefinition- the Spring transaction definition that defines semantics- Returns:
- an arbitrary object that holds transaction data, if any
(to be passed into
JpaDialect.cleanupTransaction(Object)). May implement theSavepointManagerinterface. - Throws:
jakarta.persistence.PersistenceException- if thrown by JPA methodsSQLException- if thrown by JDBC methodsTransactionException- in case of invalid arguments- See Also:
-
prepareTransaction
public @Nullable Object prepareTransaction(jakarta.persistence.EntityManager entityManager, boolean readOnly, @Nullable String name) throws jakarta.persistence.PersistenceException This implementation returns transaction data for a flush mode reset if necessary, callingprepareFlushMode(EntityManager, boolean)accordingly.- Specified by:
prepareTransactionin interfaceJpaDialect- Parameters:
entityManager- the EntityManager to begin a JPA transaction onreadOnly- whether the transaction is supposed to be read-onlyname- the name of the transaction (if any)- Returns:
- an arbitrary object that holds transaction data, if any
(to be passed into
JpaDialect.cleanupTransaction(Object)) - Throws:
jakarta.persistence.PersistenceException- if thrown by JPA methods- See Also:
-
prepareFlushMode
protected @Nullable Object prepareFlushMode(jakarta.persistence.EntityManager entityManager, boolean readOnly) Prepare transaction data for a flush mode reset if necessary. Only applied for read-only transactions on JPA 4.0.Used by
beginTransaction(EntityManager, TransactionDefinition)as well asprepareTransaction(EntityManager, boolean, String). Can be reused in corresponding overridden methods in vendor-specific subclasses, or alternatively replaced with custom flush mode handling.- Parameters:
entityManager- the EntityManager to begin a JPA transaction onreadOnly- whether the transaction is supposed to be read-only- Returns:
- transaction data for a flush mode reset, if necessary
(to be returned from
beginTransaction(EntityManager, TransactionDefinition)/prepareTransaction(EntityManager, boolean, String)and subsequently passed intocleanupTransaction(Object)after completion) - Since:
- 7.0.6
-
cleanupTransaction
This implementation resets the flush mode if necessary.- Specified by:
cleanupTransactionin interfaceJpaDialect- Parameters:
transactionData- arbitrary object that holds transaction data, if any (as returned by beginTransaction or prepareTransaction)- See Also:
-
getJdbcConnection
public @Nullable ConnectionHandle getJdbcConnection(jakarta.persistence.EntityManager entityManager, boolean readOnly) throws jakarta.persistence.PersistenceException, SQLException This implementation always returnsnull, indicating that no JDBC Connection can be provided.- Specified by:
getJdbcConnectionin interfaceJpaDialect- Parameters:
entityManager- the current JPA EntityManagerreadOnly- whether the Connection is only needed for read-only purposes- Returns:
- a handle for the Connection, to be passed into
releaseJdbcConnection, ornullif no JDBC Connection can be retrieved - Throws:
jakarta.persistence.PersistenceException- if thrown by JPA methodsSQLException- if thrown by JDBC methods- See Also:
-
releaseJdbcConnection
public void releaseJdbcConnection(ConnectionHandle conHandle, jakarta.persistence.EntityManager em) throws jakarta.persistence.PersistenceException, SQLException This implementation does nothing, assuming that the Connection will implicitly be closed with the EntityManager.If the JPA implementation returns a Connection handle that it expects the application to close after use, the dialect implementation needs to invoke
Connection.close()(or some other method with similar effect) here.- Specified by:
releaseJdbcConnectionin interfaceJpaDialect- Parameters:
conHandle- the JDBC Connection handle to releaseem- the current JPA EntityManager- Throws:
jakarta.persistence.PersistenceException- if thrown by JPA methodsSQLException- if thrown by JDBC methods- See Also:
-
deriveEntityAgent
public @Nullable Object deriveEntityAgent(jakarta.persistence.EntityManager entityManager, @Nullable Map<?, ?> properties) throws jakarta.persistence.PersistenceExceptionDerive a newEntityAgentfrom the givenEntityManagerif possible, sharing the transactional context.NOTE: This method is designed for Spring's early JPA 4.0 support. For JPA 3.2 compatibility, the return type cannot be enforced as
jakarta.persistence.EntityAgent. Subclasses should override it with their specific EntityAgent type if possible, or otherwise just EntityAgent, as a covariant return type. This will make it forward-compatible with a future variant of this method in theJpaDialectinterface itself.- Parameters:
entityManager- the current JPA EntityManagerproperties- the properties for the EntityAgent, if any- Returns:
- the new EntityAgent instance, or
nullif none can be derived - Throws:
jakarta.persistence.PersistenceException- if thrown by JPA methods- Since:
- 7.0.4
-
translateExceptionIfPossible
This implementation delegates to EntityManagerFactoryUtils.- Specified by:
translateExceptionIfPossiblein interfacePersistenceExceptionTranslator- Parameters:
ex- a RuntimeException to translate- Returns:
- the corresponding DataAccessException (or
nullif the exception could not be translated, as in this case it may result from user code rather than from an actual persistence problem) - See Also:
-