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

public Class HibernateJpaDialect

extends DefaultJpaDialect
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.sql.Connection, .SQLException, jakarta.persistence.EntityManager, .PersistenceException, org.hibernate.ConnectionReleaseMode, .FlushMode, .HibernateException, .JDBCException, .NonUniqueObjectException, .NonUniqueResultException, .ObjectDeletedException, .PersistentObjectException, .PessimisticLockException, .PropertyValueException, .QueryException, .QueryTimeoutException, .Session, .StaleObjectStateException, .StaleStateException, .TransientObjectException, .UnresolvableObjectException, .WrongClassException, org.hibernate.dialect.lock.OptimisticEntityLockException, .PessimisticEntityLockException, org.hibernate.engine.spi.SessionImplementor, org.hibernate.exception.ConstraintViolationException, .DataException, .JDBCConnectionException, .LockAcquisitionException, .SQLGrammarException, org.springframework.dao.CannotAcquireLockException, .DataAccessException, .DataAccessResourceFailureException, .DataIntegrityViolationException, .DuplicateKeyException, .IncorrectResultSizeDataAccessException, .InvalidDataAccessApiUsageException, .InvalidDataAccessResourceUsageException, .PessimisticLockingFailureException, org.springframework.jdbc.datasource.ConnectionHandle, .DataSourceUtils, org.springframework.jdbc.support.SQLExceptionSubclassTranslator, .SQLExceptionTranslator, org.springframework.lang.Nullable, org.springframework.orm.ObjectOptimisticLockingFailureException, .ObjectRetrievalFailureException, org.springframework.orm.jpa.DefaultJpaDialect, .EntityManagerFactoryUtils, .JpaSystemException, org.springframework.transaction.InvalidIsolationLevelException, .TransactionDefinition, .TransactionException, org.springframework.transaction.support.ResourceTransactionDefinition, org.springframework.util.ReflectionUtils

org.springframework.orm.jpa.JpaDialect implementation for Hibernate. Compatible with Hibernate ORM 5.5/5.6 as well as 6.0/6.1/6.2/6.3.
Authors
Juergen Hoeller, Costin Leau
Since
2.0
See Also
HibernateJpaVendorAdapter, org.hibernate.Session#setHibernateFlushMode, org.hibernate.Transaction#setTimeout

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private SQLExceptionTranslator
pack-priv boolean
private SQLExceptionTranslator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
beginTransaction(EntityManager
the EntityManager to begin a JPA transaction on
entityManager
,
TransactionDefinition
the Spring transaction definition that defines semantics
definition
)

Overrides org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction.

Implements org.springframework.orm.jpa.JpaDialect.beginTransaction.

This implementation invokes the standard JPA Transaction.begin method.
public void
cleanupTransaction(Object
arbitrary object that holds transaction data, if any (as returned by beginTransaction or prepareTransaction)
transactionData
)

Overrides org.springframework.orm.jpa.DefaultJpaDialect.cleanupTransaction.

Implements org.springframework.orm.jpa.JpaDialect.cleanupTransaction.

This implementation does nothing, since the default beginTransaction implementation does not require any cleanup.
protected DataAccessException

Returns:

the corresponding DataAccessException instance
convertHibernateAccessException
(HibernateException
the HibernateException that occurred
ex
)

Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

protected Object
public ConnectionHandle
getJdbcConnection(EntityManager
the current JPA EntityManager
entityManager
,
boolean
whether the Connection is only needed for read-only purposes
readOnly
)

Overrides org.springframework.orm.jpa.DefaultJpaDialect.getJdbcConnection.

Implements org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

This implementation always returns null, indicating that no JDBC Connection can be provided.
protected SessionImplementor
getSession(EntityManager entityManager)

Deprecated as a consequence of SessionImplementor being deprecated.
protected FlushMode
prepareFlushMode(Session session, boolean readOnly)

Deprecated as a consequence of Session being deprecated.
public Object
prepareTransaction(EntityManager
the EntityManager to begin a JPA transaction on
entityManager
,
boolean
whether the transaction is supposed to be read-only
readOnly
,
String
the name of the transaction (if any)
name
)

Overrides org.springframework.orm.jpa.DefaultJpaDialect.prepareTransaction.

Implements org.springframework.orm.jpa.JpaDialect.prepareTransaction.

Prepare a JPA transaction, applying the specified semantics.
public void
setJdbcExceptionTranslator(SQLExceptionTranslator
the SQLExceptionTranslator to delegate to, or null for none. By default, a SQLExceptionSubclassTranslator will be used for org.hibernate.TransactionException translation as of 6.1; this can be reverted to pre-6.1 behavior through setting null here.
exceptionTranslator
)

Set the JDBC exception translator for Hibernate exception translation purposes.

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.

public DataAccessException
Inherited from org.springframework.orm.jpa.DefaultJpaDialect:
releaseJdbcConnection

Field Detail

jdbcExceptionTranslatorback to summary
private SQLExceptionTranslator jdbcExceptionTranslator
Annotations
@Nullable
prepareConnectionback to summary
pack-priv boolean prepareConnection
transactionExceptionTranslatorback to summary
private SQLExceptionTranslator transactionExceptionTranslator
Annotations
@Nullable

Constructor Detail

HibernateJpaDialectback to summary
public HibernateJpaDialect()

Method Detail

beginTransactionback to summary
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException

Overrides org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction.

Implements org.springframework.orm.jpa.JpaDialect.beginTransaction.

Doc from org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction.

This implementation invokes the standard JPA Transaction.begin method. Throws an InvalidIsolationLevelException if a non-default isolation level is set.

This implementation does not return any transaction data Object, since there is no state to be kept for a standard JPA transaction. Hence, subclasses do not have to care about the return value (null) of this implementation and are free to return their own transaction data Object.

Parameters
entityManager:EntityManager

Doc from org.springframework.orm.jpa.JpaDialect.beginTransaction.

the EntityManager to begin a JPA transaction on

definition:TransactionDefinition

Doc from org.springframework.orm.jpa.JpaDialect.beginTransaction.

the Spring transaction definition that defines semantics

Returns:Object

Doc from org.springframework.orm.jpa.JpaDialect.beginTransaction.

an arbitrary object that holds transaction data, if any (to be passed into cleanupTransaction). May implement the org.springframework.transaction.SavepointManager interface.

Annotations
@Override
Exceptions
PersistenceException:

Doc from org.springframework.orm.jpa.JpaDialect.beginTransaction.

if thrown by JPA methods

SQLException:

Doc from org.springframework.orm.jpa.JpaDialect.beginTransaction.

if thrown by JDBC methods

TransactionException:

Doc from org.springframework.orm.jpa.JpaDialect.beginTransaction.

in case of invalid arguments

cleanupTransactionback to summary
public void cleanupTransaction(Object transactionData)

Overrides org.springframework.orm.jpa.DefaultJpaDialect.cleanupTransaction.

Implements org.springframework.orm.jpa.JpaDialect.cleanupTransaction.

Doc from org.springframework.orm.jpa.DefaultJpaDialect.cleanupTransaction.

This implementation does nothing, since the default beginTransaction implementation does not require any cleanup.

Parameters
transactionData:Object

Doc from org.springframework.orm.jpa.JpaDialect.cleanupTransaction.

arbitrary object that holds transaction data, if any (as returned by beginTransaction or prepareTransaction)

Annotations
@Override
convertHibernateAccessExceptionback to summary
protected DataAccessException convertHibernateAccessException(HibernateException ex)

Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

Parameters
ex:HibernateException

the HibernateException that occurred

Returns:DataAccessException

the corresponding DataAccessException instance

getIdentifierback to summary
protected Object getIdentifier(HibernateException hibEx)
Annotations
@Nullable
getJdbcConnectionback to summary
public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException

Overrides org.springframework.orm.jpa.DefaultJpaDialect.getJdbcConnection.

Implements org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

Doc from org.springframework.orm.jpa.DefaultJpaDialect.getJdbcConnection.

This implementation always returns null, indicating that no JDBC Connection can be provided.

Parameters
entityManager:EntityManager

Doc from org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

the current JPA EntityManager

readOnly:boolean

Doc from org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

whether the Connection is only needed for read-only purposes

Returns:ConnectionHandle

Doc from org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

a handle for the Connection, to be passed into releaseJdbcConnection, or null if no JDBC Connection can be retrieved

Annotations
@Override
Exceptions
PersistenceException:

Doc from org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

if thrown by JPA methods

SQLException:

Doc from org.springframework.orm.jpa.JpaDialect.getJdbcConnection.

if thrown by JDBC methods

getSessionback to summary
protected SessionImplementor getSession(EntityManager entityManager)

Deprecated

as a consequence of SessionImplementor being deprecated. See corresponding docs for further information.

prepareFlushModeback to summary
protected FlushMode prepareFlushMode(Session session, boolean readOnly) throws PersistenceException

Deprecated

as a consequence of Session being deprecated. See corresponding docs for further information.

Annotations
@Nullable
prepareTransactionback to summary
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name) throws PersistenceException

Overrides org.springframework.orm.jpa.DefaultJpaDialect.prepareTransaction.

Implements org.springframework.orm.jpa.JpaDialect.prepareTransaction.

Doc from org.springframework.orm.jpa.JpaDialect.prepareTransaction.

Prepare a JPA transaction, applying the specified semantics. Called by EntityManagerFactoryUtils when enlisting an EntityManager in a JTA transaction or a locally joined transaction (e.g. after upgrading an unsynchronized EntityManager to a synchronized one).

An implementation can apply the read-only flag as flush mode. In that case, a transaction data object can be returned that holds the previous flush mode (and possibly other data), to be reset in cleanupTransaction.

Implementations can also use the Spring transaction name to optimize for specific data access use cases (effectively using the current transaction name as use case identifier).

Parameters
entityManager:EntityManager

the EntityManager to begin a JPA transaction on

readOnly:boolean

whether the transaction is supposed to be read-only

name:String

the name of the transaction (if any)

Returns:Object

an arbitrary object that holds transaction data, if any (to be passed into cleanupTransaction)

Annotations
@Override
Exceptions
PersistenceException:
if thrown by JPA methods
setJdbcExceptionTranslatorback to summary
public void setJdbcExceptionTranslator(SQLExceptionTranslator exceptionTranslator)

Set the JDBC exception translator for Hibernate exception translation purposes.

Applied to any detected java.sql.SQLException root cause of a Hibernate JDBCException, overriding Hibernate's own SQLException translation (which is based on a Hibernate Dialect for a specific target database).

As of 6.1, also applied to org.hibernate.TransactionException translation with a SQLException root cause (where Hibernate does not translate itself at all), overriding Spring's default SQLExceptionSubclassTranslator there.

Parameters
exceptionTranslator:SQLExceptionTranslator

the SQLExceptionTranslator to delegate to, or null for none. By default, a SQLExceptionSubclassTranslator will be used for org.hibernate.TransactionException translation as of 6.1; this can be reverted to pre-6.1 behavior through setting null here.

Since
5.1
See Also
java.sql.SQLException, org.hibernate.JDBCException, org.springframework.jdbc.support.SQLExceptionSubclassTranslator, org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
setPrepareConnectionback to summary
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
java.sql.Connection#setTransactionIsolation, java.sql.Connection#setReadOnly
translateExceptionIfPossibleback to summary
public DataAccessException translateExceptionIfPossible(RuntimeException ex)

Overrides org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible.

Implements org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible.

Doc from org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible.

This implementation delegates to EntityManagerFactoryUtils.

Parameters
ex:RuntimeException

Doc from org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible.

a RuntimeException to translate

Returns:DataAccessException

Doc from org.springframework.dao.support.PersistenceExceptionTranslator.translateExceptionIfPossible.

the corresponding DataAccessException (or null if the exception could not be translated, as in this case it may result from user code rather than from an actual persistence problem)

Annotations
@Override
@Nullable
org.springframework.orm.jpa.vendor back to summary

private Class HibernateJpaDialect.HibernateConnectionHandle

extends Object
implements ConnectionHandle
Class Inheritance
  • java.lang.Object
  • org.springframework.orm.jpa.vendor.HibernateJpaDialect.HibernateConnectionHandle
All Implemented Interfaces
org.springframework.jdbc.datasource.ConnectionHandle

Field Summary

Modifier and TypeField and Description
private final SessionImplementor
session

Deprecated as a consequence of SessionImplementor being deprecated.

Constructor Summary

AccessConstructor and Description
public
HibernateConnectionHandle(SessionImplementor session)

Deprecated as a consequence of SessionImplementor being deprecated.

Method Summary

Modifier and TypeMethod and Description
public Connection
getConnection()

Implements org.springframework.jdbc.datasource.ConnectionHandle.getConnection.

Fetch the JDBC Connection that this handle refers to.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

sessionback to summary
private final SessionImplementor session

Deprecated

as a consequence of SessionImplementor being deprecated. See corresponding docs for further information.

Constructor Detail

HibernateConnectionHandleback to summary
public HibernateConnectionHandle(SessionImplementor session)

Deprecated

as a consequence of SessionImplementor being deprecated. See corresponding docs for further information.

Method Detail

getConnectionback to summary
public Connection getConnection()

Implements org.springframework.jdbc.datasource.ConnectionHandle.getConnection.

Doc from org.springframework.jdbc.datasource.ConnectionHandle.getConnection.

Fetch the JDBC Connection that this handle refers to.

Annotations
@Override
org.springframework.orm.jpa.vendor back to summary

private Class HibernateJpaDialect.SessionTransactionData

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.orm.jpa.vendor.HibernateJpaDialect.SessionTransactionData

Field Summary

Modifier and TypeField and Description
private final boolean
private final FlushMode
private final Integer
private final boolean
private final SessionImplementor
session

Deprecated as a consequence of SessionImplementor being deprecated.

Constructor Summary

AccessConstructor and Description
public
SessionTransactionData(SessionImplementor session, FlushMode previousFlushMode, boolean connectionPrepared, Integer previousIsolationLevel, boolean readOnly)

Deprecated as a consequence of SessionImplementor being deprecated.

Method Summary

Modifier and TypeMethod and Description
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

needsConnectionResetback to summary
private final boolean needsConnectionReset
previousFlushModeback to summary
private final FlushMode previousFlushMode
Annotations
@Nullable
previousIsolationLevelback to summary
private final Integer previousIsolationLevel
Annotations
@Nullable
readOnlyback to summary
private final boolean readOnly
sessionback to summary
private final SessionImplementor session

Deprecated

as a consequence of SessionImplementor being deprecated. See corresponding docs for further information.

Constructor Detail

SessionTransactionDataback to summary
public SessionTransactionData(SessionImplementor session, FlushMode previousFlushMode, boolean connectionPrepared, Integer previousIsolationLevel, boolean readOnly)

Deprecated

as a consequence of SessionImplementor being deprecated. See corresponding docs for further information.

Method Detail

resetSessionStateback to summary
public void resetSessionState()