I needed to set the generate_statistics property for Hibernate, with use with the excellent Hibernate JConsole Plugin.
The issue is that I am using Spring and JPA, and I wanted to set this property in my Spring XML configuration file. Most of the Spring JPA examples just show you how to set the properties exposed by the HibernateJpaVendorAdapter, e.g.
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="a.b.c.d" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="false" /> <property name="generateDdl" value="false" /> <property name="databasePlatform" value="SQLServer2008Dialect" /> </bean> </property> </bean>
So how can we set other Hibernate session properties? It turns out that we can set the jpaPropertyMap property of the parent LocalContainerEntityManagerFactoryBean also:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="a.b.c.d" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="false" /> <property name="generateDdl" value="false" /> <property name="databasePlatform" value="SQLServer2008Dialect" /> </bean> </property> <property name="jpaPropertyMap"> <map> <entry key="hibernate.generate_statistics" value="true"/> </map> </property> </bean> </bean>
Now we are free to set Hiberate session properties. I found this technique used on the Spring Forums, as part of other solutions, but wanted to emphasis the basic technique for others who were similarly stumped.