public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter
JpaVendorAdapter
implementation for Hibernate
EntityManager. Developed and tested against Hibernate 5.3 and 5.4;
backwards-compatible with Hibernate 5.2 at runtime on a best-effort basis.
Exposes Hibernate's persistence provider and Hibernate's Session as extended
EntityManager interface, and adapts AbstractJpaVendorAdapter
's common
configuration settings. Also supports the detection of annotated packages (through
SmartPersistenceUnitInfo.getManagedPackages()
),
e.g. containing Hibernate FilterDef
annotations,
along with Spring-driven entity scanning which requires no persistence.xml
(LocalContainerEntityManagerFactoryBean.setPackagesToScan(java.lang.String...)
).
A note about HibernateJpaVendorAdapter
vs native Hibernate settings:
Some settings on this adapter may conflict with native Hibernate configuration rules
or custom Hibernate properties. For example, specify either AbstractJpaVendorAdapter.setDatabase(org.springframework.orm.jpa.vendor.Database)
or
Hibernate's "hibernate.dialect_resolvers" property, not both. Also, be careful about
Hibernate's connection release mode: This adapter prefers ON_CLOSE
behavior,
aligned with HibernateJpaDialect.setPrepareConnection(boolean)
, at least for non-JTA
scenarios; you may override this through corresponding native Hibernate properties.
HibernateJpaDialect
Constructor and Description |
---|
HibernateJpaVendorAdapter() |
Modifier and Type | Method and Description |
---|---|
protected Class<?> |
determineDatabaseDialectClass(Database database)
Determine the Hibernate database dialect class for the given target database.
|
Class<? extends EntityManagerFactory> |
getEntityManagerFactoryInterface()
Return the vendor-specific EntityManagerFactory interface
that the EntityManagerFactory proxy is supposed to implement.
|
Class<? extends EntityManager> |
getEntityManagerInterface()
Return the vendor-specific EntityManager interface
that this provider's EntityManagers will implement.
|
HibernateJpaDialect |
getJpaDialect()
Return the vendor-specific JpaDialect implementation for this
provider, or
null if there is none. |
Map<String,Object> |
getJpaPropertyMap()
Return a Map of vendor-specific JPA properties,
typically based on settings in this JpaVendorAdapter instance.
|
Map<String,Object> |
getJpaPropertyMap(PersistenceUnitInfo pui)
Return a Map of vendor-specific JPA properties for the given persistence
unit, typically based on settings in this JpaVendorAdapter instance.
|
PersistenceProvider |
getPersistenceProvider()
Return the vendor-specific persistence provider.
|
String |
getPersistenceProviderRootPackage()
Return the name of the persistence provider's root package
(e.g.
|
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.
|
getDatabase, getDatabasePlatform, isGenerateDdl, isShowSql, postProcessEntityManager, postProcessEntityManagerFactory, setDatabase, setDatabasePlatform, setGenerateDdl, setShowSql
public void setPrepareConnection(boolean prepareConnection)
See HibernateJpaDialect.setPrepareConnection(boolean)
for details.
This is just a convenience flag passed through to HibernateJpaDialect
.
On Hibernate 5.1+, this flag remains true
by default like against
previous Hibernate versions. The vendor adapter manually enforces Hibernate's
new connection handling mode DELAYED_ACQUISITION_AND_HOLD
in that case
unless a user-specified connection handling mode property indicates otherwise;
switch this flag to false
to avoid that interference.
NOTE: For a persistence unit with transaction type JTA e.g. on WebLogic,
the connection release mode will never be altered from its provider default,
i.e. not be forced to DELAYED_ACQUISITION_AND_HOLD
by this flag.
Alternatively, set Hibernate's "hibernate.connection.handling_mode"
property to "DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION" or even
"DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT" in such a scenario.
public PersistenceProvider getPersistenceProvider()
JpaVendorAdapter
public String getPersistenceProviderRootPackage()
JpaVendorAdapter
getPersistenceProviderRootPackage
in interface JpaVendorAdapter
getPersistenceProviderRootPackage
in class AbstractJpaVendorAdapter
public Map<String,Object> getJpaPropertyMap(PersistenceUnitInfo pui)
JpaVendorAdapter
Note that there might be further JPA properties defined on the EntityManagerFactory bean, which might potentially override individual JPA property values specified here.
This implementation delegates to JpaVendorAdapter.getJpaPropertyMap()
for
non-unit-dependent properties. Effectively, this PersistenceUnitInfo-based
variant only needs to be implemented if there is an actual need to react
to unit-specific characteristics such as the transaction type.
NOTE: This variant will only be invoked in case of Java EE style
container bootstrapping where a PersistenceUnitInfo
is present
(i.e. LocalContainerEntityManagerFactoryBean
. In case of simple
Java SE style bootstrapping via Persistence
(i.e. LocalEntityManagerFactoryBean
), the parameter-less
JpaVendorAdapter.getJpaPropertyMap()
variant will be called directly.
getJpaPropertyMap
in interface JpaVendorAdapter
getJpaPropertyMap
in class AbstractJpaVendorAdapter
pui
- the PersistenceUnitInfo for the current persistence unitPersistenceUnitInfo.getTransactionType()
,
PersistenceProvider.createContainerEntityManagerFactory(PersistenceUnitInfo, Map)
public Map<String,Object> getJpaPropertyMap()
JpaVendorAdapter
Note that there might be further JPA properties defined on the EntityManagerFactory bean, which might potentially override individual JPA property values specified here.
getJpaPropertyMap
in interface JpaVendorAdapter
getJpaPropertyMap
in class AbstractJpaVendorAdapter
Persistence.createEntityManagerFactory(String, Map)
@Nullable protected Class<?> determineDatabaseDialectClass(Database database)
database
- the target databasenull
if none foundpublic HibernateJpaDialect getJpaDialect()
JpaVendorAdapter
null
if there is none.getJpaDialect
in interface JpaVendorAdapter
getJpaDialect
in class AbstractJpaVendorAdapter
public Class<? extends EntityManagerFactory> getEntityManagerFactoryInterface()
JpaVendorAdapter
If the provider does not offer any EntityManagerFactory extensions,
the adapter should simply return the standard
EntityManagerFactory
class here.
getEntityManagerFactoryInterface
in interface JpaVendorAdapter
getEntityManagerFactoryInterface
in class AbstractJpaVendorAdapter
public Class<? extends EntityManager> getEntityManagerInterface()
JpaVendorAdapter
If the provider does not offer any EntityManager extensions,
the adapter should simply return the standard
EntityManager
class here.
getEntityManagerInterface
in interface JpaVendorAdapter
getEntityManagerInterface
in class AbstractJpaVendorAdapter