Introduction
To inspect the Persistence Context and the currently executing queries, Hypersistence Optimizer needs to decorate the JPA EntityManagerFactory or the Hibernate SessionFactory and inject various performance-related checks at runtime.
If you’re using Hibernate 5 or newer versions, you don’t need this guide, as the Hibernate SessionFactory is decorated automatically, as explained in the Installation Guide.
Since the SessionFactoryBuilderFactory mechanism was introduced in Hibernate 5, if you’re using Hibernate 4 or 3, you will need to decorate the JPA EntityManagerFactory or the Hibernate SessionFactory manually, as illustrated by the following configurations.
Decorating the Persistence Unit using Spring and Hibernate 4 and 3
If you’re using Spring, you can use either the Java-based configuration or the legacy XML configuration files. The following configuration examples will show you how to decorate the Persistence Unit manually using Spring when using either Hibernate 4 or 3.
Decorating the Persistence Unit using Spring and Hibernate 4
If you’re using Hibernate 4, then the Persistence Unit can be created either via a LocalSessionFactoryBean or the LocalContainerEntityManagerFactoryBean.
Decorating the Hibernate 4 SessionFactory programmatically with Spring Java-based configuration
To decorate the Hibernate SessionFactory, you just need to pass the SessionFactory reference to the decorate method of the HypersistenceHibernatePersistenceProvider class. For this reason, the LocalSessionFactoryBean creates a bean, called originalSessionFactory.
This is the bean you would have normally named sessionFactory, but, in this case, it needs to have a different name as the sessionFactory bean name is reserved for the Hypersistence Optimizer SessionFactory, as illustrated by the following configuration:
@Bean
public LocalSessionFactoryBean originalSessionFactory() {
LocalSessionFactoryBean localSessionFactoryBean =
new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource());
localSessionFactoryBean.setPackagesToScan(packagesToScan());
localSessionFactoryBean.setHibernateProperties(additionalProperties());
return localSessionFactoryBean;
}
@Bean
@Primary
public SessionFactory sessionFactory(
SessionFactory originalSessionFactory) {
return HypersistenceHibernatePersistenceProvider.decorate(
originalSessionFactory
);
}
First, we create the LocalSessionFactoryBean, which will be used to create a SessionFactory that’s decorated by the HypersistenceHibernatePersistenceProvider class.
Notice that we used the @Primary annotation on the decorated sessionFactory bean method to instruct Spring that this bean should be used whenever a SessionFactory dependency is needed.
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-hibernate4-exampleGitHub repository.
Decorating the Hibernate 4 SessionFactory declaratively with Spring XML configuration
To decorate the Hibernate SessionFactory using the legacy XML configuration, you just need to wrap the original SessionFactory as illustrated by the following examples.
<bean id="originalSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>${packages.to.scan}</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="sessionFactory"
class="io.hypersistence.optimizer.hibernate.decorator.HypersistenceHibernatePersistenceProvider"
factory-method="decorate"
primary="true">
<constructor-arg ref="originalSessionFactory"/>
</bean>
First, we create the LocalSessionFactoryBean, which will be used to create a SessionFactory that’s decorated by the HypersistenceHibernatePersistenceProvider class.
Notice that we set the primary attribute to true on the decorated sessionFactory bean configuration to instruct Spring that this bean should be used whenever a SessionFactory dependency is needed.
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-hibernate4-exampleGitHub repository.
Decorating the Hibernate 4 JPA EntityManagerFactory programmatically with Spring Java-based configuration
To decorate the JPA EntityManagerFactory, you just need to pass the EntityManagerFactory reference to the decorate method of the HypersistenceHibernatePersistenceProvider class:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPersistenceUnitName(persistenceUnitName());
entityManagerFactoryBean.setPersistenceProvider(new HibernatePersistenceProvider());
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(packagesToScan());
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
entityManagerFactoryBean.setJpaProperties(additionalProperties());
return entityManagerFactoryBean;
}
@Bean
@Primary
public EntityManagerFactory entityManagerFactory(
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
return HypersistenceHibernatePersistenceProvider.decorate(
entityManagerFactoryBean.getNativeEntityManagerFactory()
);
}
First, we create the LocalContainerEntityManagerFactoryBean, which will be used to create an EntityManagerFactory that’s decorated by the HypersistenceHibernatePersistenceProvider class.
Notice that we used the @Primary annotation on the decorated entityManagerFactory bean method to instruct Spring that this bean should be used whenever an EntityManagerFactory dependency is needed.
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-jpa-hibernate4-exampleGitHub repository.
Decorating the Hibernate 4 JPA EntityManagerFactory declaratively with Spring XML configuration
To decorate the JPA EntityManagerFactory using the legacy XML configuration, you just need to wrap the original EntityManagerFactory as illustrated by the following examples.
<bean id="entityManagerFactory"
class="io.hypersistence.optimizer.hibernate.decorator.HypersistenceHibernatePersistenceProvider"
factory-method="decorate">
<constructor-arg>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>${packages.to.scan}</value>
</list>
</property>
<property name="jpaVendorAdapter" ref="jpaVendor"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
</constructor-arg>
</bean>
Notice that we haven’t defined a LocalContainerEntityManagerFactoryBean bean that will be decorated by the primary entityManagerFactory, as we did for the Java-based configuration.
This time, we are just passing the LocalContainerEntityManagerFactoryBean to the decorate method of the HypersistenceHibernatePersistenceProvider utility in order to create the EntityManagerFactory bean.
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-hibernate4-exampleGitHub repository.
Decorating the Persistence Unit using Spring and Hibernate 3
If you’re using Hibernate 3, then the Persistence Unit can be created either via an AnnotationSessionFactoryBean or the LocalContainerEntityManagerFactoryBean.
Decorating the Hibernate 3 SessionFactory programmatically with Spring Java-based configuration
To decorate the Hibernate SessionFactory, you just need to pass the SessionFactory reference to the decorate method of the HypersistenceHibernatePersistenceProvider class. For this reason, the AnnotationSessionFactoryBean creates a bean, called originalSessionFactory.
This is the bean you would have normally named sessionFactory, but, in this case, it needs to have a different name as the sessionFactory bean name is reserved for the Hypersistence Optimizer SessionFactory, as illustrated by the following configuration:
@Bean
public AnnotationSessionFactoryBean originalSessionFactory() {
AnnotationSessionFactoryBean localSessionFactoryBean = new AnnotationSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource());
localSessionFactoryBean.setPackagesToScan(packagesToScan());
localSessionFactoryBean.setHibernateProperties(additionalProperties());
return localSessionFactoryBean;
}
@Bean
public SessionFactory sessionFactory(SessionFactory originalSessionFactory) {
return HypersistenceHibernatePersistenceProvider.decorate(
originalSessionFactory
);
}
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-hibernate3-exampleGitHub repository.
Decorating the Hibernate 3 SessionFactory with Spring XML configuration
If you’re using Hibernate 3, you can use the following XML configuration to decorate the Hibernate SessionFactory:
<bean id="originalSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>${packages.to.scan}</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="sessionFactory"
class="io.hypersistence.optimizer.hibernate.decorator.HypersistenceHibernatePersistenceProvider"
factory-method="decorate">
<constructor-arg ref="originalSessionFactory"/>
</bean>
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-hibernate3-exampleGitHub repository.
Decorating the Hibernate 3 JPA EntityManagerFactory programmatically with Spring Java-based configuration
To decorate the JPA EntityManagerFactory, you just need to pass the EntityManagerFactory reference to the decorate method of the HypersistenceHibernatePersistenceProvider class:
@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean =
new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPersistenceUnitName(persistenceUnitName());
entityManagerFactoryBean.setPersistenceProvider(new HibernatePersistence());
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(packagesToScan());
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
entityManagerFactoryBean.setJpaProperties(additionalProperties());
entityManagerFactoryBean.afterPropertiesSet();
return HypersistenceHibernatePersistenceProvider.decorate(
entityManagerFactoryBean.getNativeEntityManagerFactory()
);
}
To avoid creating two different beans of the EntityManagerFactory type, that will prevent you from using the @PersistenceUnit application without an explicit name qualifier, you can create the LocalContainerEntityManagerFactoryBean and initialize it by calling afterPropertiesSet prior to passing the resulting EntityManagerFactory to the decorate method.
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-jpa-hibernate3-exampleGitHub repository.
Decorating the Hibernate 3 JPA EntityManagerFactory declaratively with Spring XML configuration
To decorate the JPA EntityManagerFactory using the legacy XML configuration, you just need to wrap the original EntityManagerFactory as illustrated by the following examples.
<bean id="entityManagerFactory"
class="io.hypersistence.optimizer.hibernate.decorator.HypersistenceHibernatePersistenceProvider"
factory-method="decorate">
<constructor-arg>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>${packages.to.scan}</value>
</list>
</property>
<property name="jpaVendorAdapter" ref="jpaVendor"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
</constructor-arg>
</bean>
Notice that we are just passing the LocalContainerEntityManagerFactoryBean to the decorate method of the HypersistenceHibernatePersistenceProvider utility in order to create the EntityManagerFactory bean.
You can find a working example using the above configuration in the
hypersistence-optimizer-spring-jpa-hibernate3-exampleGitHub repository.
Decorating the JPA EntityManagerFactory declaratively
To decorate the JPA EntityManagerFactory declaratively using the persistence.xml configuration file, you need to use the HypersistenceHibernatePersistenceProvider class, as illustrated by the provider XML tag in the following example:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="JTA">
<provider>io.hypersistence.optimizer.hibernate.decorator.HypersistenceHibernatePersistenceProvider</provider>
<jta-data-source>java:global/jdbc/default</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
You can find a working example using the above configuration in the
hypersistence-optimizer-glassfish-hibernate4-exampleGitHub repository.
Instantiating HypersistenceOptimizer
After decorating the Persistence Unit, you can go back to the Installation Guide and instantiate the HypersistenceOptimizer object.