Class DefaultJpaDialect

java.lang.Object
org.springframework.orm.jpa.DefaultJpaDialect
All Implemented Interfaces:
Serializable, org.springframework.dao.support.PersistenceExceptionTranslator, JpaDialect
Direct Known Subclasses:
EclipseLinkJpaDialect, HibernateJpaDialect

public class DefaultJpaDialect extends Object implements JpaDialect, Serializable
Default implementation of the 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 Details

    • DefaultJpaDialect

      public DefaultJpaDialect()
  • Method Details

    • beginTransaction

      public @Nullable Object beginTransaction(jakarta.persistence.EntityManager entityManager, org.springframework.transaction.TransactionDefinition definition) throws jakarta.persistence.PersistenceException, SQLException, org.springframework.transaction.TransactionException
      This implementation invokes the standard JPA Transaction.begin method. 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:
      beginTransaction in interface JpaDialect
      Parameters:
      entityManager - the EntityManager to begin a JPA transaction on
      definition - 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 the SavepointManager interface.
      Throws:
      jakarta.persistence.PersistenceException - if thrown by JPA methods
      SQLException - if thrown by JDBC methods
      org.springframework.transaction.TransactionException - 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, calling prepareFlushMode(EntityManager, boolean) accordingly.
      Specified by:
      prepareTransaction in interface JpaDialect
      Parameters:
      entityManager - the EntityManager to begin a JPA transaction on
      readOnly - whether the transaction is supposed to be read-only
      name - 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 as prepareTransaction(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 on
      readOnly - 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 into cleanupTransaction(Object) after completion)
      Since:
      7.0.6
    • cleanupTransaction

      public void cleanupTransaction(@Nullable Object transactionData)
      This implementation resets the flush mode if necessary.
      Specified by:
      cleanupTransaction in interface JpaDialect
      Parameters:
      transactionData - arbitrary object that holds transaction data, if any (as returned by beginTransaction or prepareTransaction)
      See Also:
    • getJdbcConnection

      public @Nullable org.springframework.jdbc.datasource.ConnectionHandle getJdbcConnection(jakarta.persistence.EntityManager entityManager, boolean readOnly) throws jakarta.persistence.PersistenceException, SQLException
      This implementation always returns null, indicating that no JDBC Connection can be provided.
      Specified by:
      getJdbcConnection in interface JpaDialect
      Parameters:
      entityManager - the current JPA EntityManager
      readOnly - whether the Connection is only needed for read-only purposes
      Returns:
      a handle for the Connection, to be passed into releaseJdbcConnection, or null if no JDBC Connection can be retrieved
      Throws:
      jakarta.persistence.PersistenceException - if thrown by JPA methods
      SQLException - if thrown by JDBC methods
      See Also:
    • releaseJdbcConnection

      public void releaseJdbcConnection(org.springframework.jdbc.datasource.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:
      releaseJdbcConnection in interface JpaDialect
      Parameters:
      conHandle - the JDBC Connection handle to release
      em - the current JPA EntityManager
      Throws:
      jakarta.persistence.PersistenceException - if thrown by JPA methods
      SQLException - if thrown by JDBC methods
      See Also:
    • deriveEntityAgent

      public @Nullable Object deriveEntityAgent(jakarta.persistence.EntityManager entityManager, @Nullable Map<?,?> properties) throws jakarta.persistence.PersistenceException
      Derive a new EntityAgent from the given EntityManager if 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 the JpaDialect interface itself.

      Parameters:
      entityManager - the current JPA EntityManager
      properties - the properties for the EntityAgent, if any
      Returns:
      the new EntityAgent instance, or null if none can be derived
      Throws:
      jakarta.persistence.PersistenceException - if thrown by JPA methods
      Since:
      7.0.4
    • translateExceptionIfPossible

      public @Nullable org.springframework.dao.DataAccessException translateExceptionIfPossible(RuntimeException ex)
      This implementation delegates to EntityManagerFactoryUtils.
      Specified by:
      translateExceptionIfPossible in interface org.springframework.dao.support.PersistenceExceptionTranslator
      See Also: