Class HibernateJpaDialect
- All Implemented Interfaces:
Serializable, PersistenceExceptionTranslator, JpaDialect
JpaDialect implementation for Hibernate.
Compatible with Hibernate ORM 7.x and 8.x.- Since:
- 2.0
- Author:
- Juergen Hoeller, Costin Leau
- 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(Session session, boolean readOnly) prepareTransaction(jakarta.persistence.EntityManager entityManager, boolean readOnly, @Nullable String name) This implementation returns transaction data for a flush mode reset if necessary, callingDefaultJpaDialect.prepareFlushMode(EntityManager, boolean)accordingly.voidsetJdbcExceptionTranslator(@Nullable SQLExceptionTranslator exceptionTranslator) Set the JDBC exception translator for Hibernate exception translation purposes.voidsetPrepareConnection(boolean prepareConnection) Set whether to prepare the underlying JDBC Connection of a transactional Hibernate Session, that is, whether to apply a transaction-specific isolation level and/or the transaction's read-only flag to the underlying JDBC Connection.This implementation delegates to EntityManagerFactoryUtils.Methods inherited from class DefaultJpaDialect
prepareFlushMode, releaseJdbcConnection
-
Constructor Details
-
HibernateJpaDialect
public HibernateJpaDialect()
-
-
Method Details
-
setPrepareConnection
public void setPrepareConnection(boolean prepareConnection) Set whether to prepare the underlying JDBC Connection of a transactional Hibernate Session, that is, whether to apply a transaction-specific isolation level and/or the transaction's read-only flag to the underlying JDBC Connection.Default is "true". If you turn this flag off, JPA transaction management will not support per-transaction isolation levels anymore. It will not call
Connection.setReadOnly(true)for read-only transactions anymore either. If this flag is turned off, no cleanup of a JDBC Connection is required after a transaction, since no Connection settings will get modified.NOTE: The default behavior in terms of read-only handling changed in Spring 4.1, propagating the read-only status to the JDBC Connection now, analogous to other Spring transaction managers. This may have the effect that you're running into read-only enforcement now where previously write access has accidentally been tolerated: Please revise your transaction declarations accordingly, removing invalid read-only markers if necessary.
- Since:
- 4.1
- See Also:
-
setJdbcExceptionTranslator
Set the JDBC exception translator for Hibernate exception translation purposes.Applied to any detected
SQLExceptionroot cause of a HibernateJDBCException, overriding Hibernate's ownSQLExceptiontranslation (which is based on a Hibernate Dialect for a specific target database).As of 6.1, also applied to
TransactionExceptiontranslation with aSQLExceptionroot cause (where Hibernate does not translate itself at all), overriding Spring's defaultSQLExceptionSubclassTranslatorthere.- Parameters:
exceptionTranslator- theSQLExceptionTranslatorto delegate to, ornullfor none. By default, aSQLExceptionSubclassTranslatorwill be used forTransactionExceptiontranslation as of 6.1; this can be reverted to pre-6.1 behavior through settingnullhere.- Since:
- 5.1
- See Also:
-
beginTransaction
public Object beginTransaction(jakarta.persistence.EntityManager entityManager, TransactionDefinition definition) throws jakarta.persistence.PersistenceException, SQLException, TransactionException Description copied from class:DefaultJpaDialectThis 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
DefaultJpaDialect.prepareFlushMode(EntityManager, boolean)accordingly. Can be reused in subclasses or alternatively replaced with custom flush mode handling.- Specified by:
beginTransactionin interfaceJpaDialect- Overrides:
beginTransactionin classDefaultJpaDialect- 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 Object prepareTransaction(jakarta.persistence.EntityManager entityManager, boolean readOnly, @Nullable String name) throws jakarta.persistence.PersistenceException Description copied from class:DefaultJpaDialectThis implementation returns transaction data for a flush mode reset if necessary, callingDefaultJpaDialect.prepareFlushMode(EntityManager, boolean)accordingly.- Specified by:
prepareTransactionin interfaceJpaDialect- Overrides:
prepareTransactionin classDefaultJpaDialect- 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
-
cleanupTransaction
Description copied from class:DefaultJpaDialectThis implementation resets the flush mode if necessary.- Specified by:
cleanupTransactionin interfaceJpaDialect- Overrides:
cleanupTransactionin classDefaultJpaDialect- Parameters:
transactionData- arbitrary object that holds transaction data, if any (as returned by beginTransaction or prepareTransaction)- See Also:
-
getJdbcConnection
public ConnectionHandle getJdbcConnection(jakarta.persistence.EntityManager entityManager, boolean readOnly) throws jakarta.persistence.PersistenceException, SQLException Description copied from class:DefaultJpaDialectThis implementation always returnsnull, indicating that no JDBC Connection can be provided.- Specified by:
getJdbcConnectionin interfaceJpaDialect- Overrides:
getJdbcConnectionin classDefaultJpaDialect- 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:
-
deriveEntityAgent
public StatelessSession deriveEntityAgent(jakarta.persistence.EntityManager entityManager, @Nullable Map<?, ?> properties) throws jakarta.persistence.PersistenceExceptionDescription copied from class:DefaultJpaDialectDerive 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.- Overrides:
deriveEntityAgentin classDefaultJpaDialect- 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
-
translateExceptionIfPossible
Description copied from class:DefaultJpaDialectThis implementation delegates to EntityManagerFactoryUtils.- Specified by:
translateExceptionIfPossiblein interfacePersistenceExceptionTranslator- Overrides:
translateExceptionIfPossiblein classDefaultJpaDialect- 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:
-