Class HibernateJpaDialect
- java.lang.Object
- org.springframework.orm.jpa.DefaultJpaDialect
- org.springframework.orm.jpa.vendor.HibernateJpaDialect
- All Implemented Interfaces:
Serializable
,PersistenceExceptionTranslator
,JpaDialect
public class HibernateJpaDialect extends DefaultJpaDialect
JpaDialect
implementation for Hibernate EntityManager. Developed and tested against Hibernate 3.6, 4.2/4.3 as well as 5.0/5.1/5.2.- Since:
- 2.0
- Author:
- Juergen Hoeller, Costin Leau
- See Also:
HibernateJpaVendorAdapter
,Session.setFlushMode(org.hibernate.FlushMode)
,Transaction.setTimeout(int)
, Serialized Form
Constructor Summary
Constructors Constructor Description HibernateJpaDialect()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
beginTransaction(EntityManager entityManager, TransactionDefinition definition)
This implementation invokes the standard JPATransaction.begin
method.void
cleanupTransaction(Object transactionData)
This implementation does nothing, since the defaultbeginTransaction
implementation does not require any cleanup.protected DataAccessException
convertHibernateAccessException(HibernateException ex)
Convert the given HibernateException to an appropriate exception from theorg.springframework.dao
hierarchy.ConnectionHandle
getJdbcConnection(EntityManager entityManager, boolean readOnly)
This implementation always returnsnull
, indicating that no JDBC Connection can be provided.protected Session
getSession(EntityManager entityManager)
protected FlushMode
prepareFlushMode(Session session, boolean readOnly)
Object
prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
Prepare a JPA transaction, applying the specified semantics.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.DataAccessException
translateExceptionIfPossible(RuntimeException ex)
This implementation delegates to EntityManagerFactoryUtils.Methods inherited from class org.springframework.orm.jpa.DefaultJpaDialect
releaseJdbcConnection
Constructor Detail
HibernateJpaDialect
public HibernateJpaDialect()
Method Detail
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" on Hibernate EntityManager 4.x (with its 'on-close' connection release mode, and "false" on Hibernate EntityManager 3.6 (due to the 'after-transaction' release mode there). Note that Hibernate 4.2+ is strongly recommended in order to make isolation levels work efficiently.
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:
Connection.setTransactionIsolation(int)
,Connection.setReadOnly(boolean)
beginTransaction
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException
Description copied from class:DefaultJpaDialect
This implementation invokes the standard JPATransaction.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.- Specified by:
beginTransaction
in interfaceJpaDialect
- Overrides:
beginTransaction
in 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(java.lang.Object)
). May implement theSavepointManager
interface. - Throws:
PersistenceException
- if thrown by JPA methodsSQLException
- if thrown by JDBC methodsTransactionException
- in case of invalid arguments- See Also:
EntityTransaction.begin()
,InvalidIsolationLevelException
,DefaultJpaDialect.cleanupTransaction(java.lang.Object)
prepareTransaction
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name) throws PersistenceException
Description copied from interface:JpaDialect
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).
- Specified by:
prepareTransaction
in interfaceJpaDialect
- Overrides:
prepareTransaction
in 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 cleanupTransaction)
- Throws:
PersistenceException
- if thrown by JPA methods- See Also:
JpaDialect.cleanupTransaction(java.lang.Object)
prepareFlushMode
protected FlushMode prepareFlushMode(Session session, boolean readOnly) throws PersistenceException
- Throws:
PersistenceException
cleanupTransaction
public void cleanupTransaction(Object transactionData)
Description copied from class:DefaultJpaDialect
This implementation does nothing, since the defaultbeginTransaction
implementation does not require any cleanup.- Specified by:
cleanupTransaction
in interfaceJpaDialect
- Overrides:
cleanupTransaction
in classDefaultJpaDialect
- Parameters:
transactionData
- arbitrary object that holds transaction data, if any (as returned by beginTransaction or prepareTransaction)- See Also:
DefaultJpaDialect.beginTransaction(javax.persistence.EntityManager, org.springframework.transaction.TransactionDefinition)
getJdbcConnection
public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly) throws PersistenceException, SQLException
Description copied from class:DefaultJpaDialect
This implementation always returnsnull
, indicating that no JDBC Connection can be provided.- Specified by:
getJdbcConnection
in interfaceJpaDialect
- Overrides:
getJdbcConnection
in 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
, ornull
if no JDBC Connection can be retrieved - Throws:
PersistenceException
- if thrown by JPA methodsSQLException
- if thrown by JDBC methods- See Also:
JpaDialect.releaseJdbcConnection(org.springframework.jdbc.datasource.ConnectionHandle, javax.persistence.EntityManager)
,ConnectionHandle.getConnection()
,SimpleConnectionHandle
,JpaTransactionManager.setDataSource(javax.sql.DataSource)
,NativeJdbcExtractor
translateExceptionIfPossible
public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Description copied from class:DefaultJpaDialect
This implementation delegates to EntityManagerFactoryUtils.- Specified by:
translateExceptionIfPossible
in interfacePersistenceExceptionTranslator
- Overrides:
translateExceptionIfPossible
in classDefaultJpaDialect
- Parameters:
ex
- a RuntimeException to translate- Returns:
- 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) - See Also:
EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(java.lang.RuntimeException)
convertHibernateAccessException
protected DataAccessException convertHibernateAccessException(HibernateException ex)
Convert the given HibernateException to an appropriate exception from theorg.springframework.dao
hierarchy.- Parameters:
ex
- HibernateException that occurred- Returns:
- the corresponding DataAccessException instance
getSession
protected Session getSession(EntityManager entityManager)