Priority between properties in HibernateJPAVendorAdapter and JPAProperty - java

I have the following configuration in an application Spring + JPA + Hibernate, using packagesToScan to avoid having file persistence.xml.
<!-- Configure JPA Implementation -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${jpa.database}" />
<property name="showSql" value="${jpa.showSql}" />
<property name="databasePlatform" value="${jpa.dialect}" />
<property name="generateDdl" value="${jpa.generateDdl}" />
</bean>
<!-- Create the JPA EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="packagesToScan">
<list>
<value>com.proyectofinal.model</value>
</list>
</property>
<property name="jpaProperty">
<props>
<entry key="hibernate.cache.use_second_level_cache" value="true"/>
<entry key="hibernate.cache.use_query_cache" value="true"/>
<entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.use_sql_comments" value="false" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.MYSQLDialect" />
<entry key="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</props>
</property>
</bean>
My question is:
Is it necessary define in both places properties like show_sql or dialect?
Which one has priority over the other?
What place is more appropriate to define it?
Thanks in advance

Properties specified in the JpaVendorAdapter don't have to be duplicated in the list of additional properties. If that would be the case the JpaVendorAdapter would be pretty useless.
Also in your configuration use either database or databasePlatform don't use both.
Properties which can be configured by using a JpaVendorAdapter I would configure right there, it would save you a couple of lines and you don't have to remember the cryptic hibernate (or which ever provider you use) property name.
The properties you need are the following.
<props>
<entry key="hibernate.cache.use_second_level_cache" value="true"/>
<entry key="hibernate.cache.use_query_cache" value="true"/>
<entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<entry key="hibernate.use_sql_comments" value="false" />
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</props>
If you define them in both places the one from the JpaVendorAdapter are ignored.

In addition to #M. Deinum's answer, if you decide to use the jpaProperties over jpaVendorAdapter, you'll need to set the persistenceProvider property since it would normally be derived from jpaVendorAdapter
for example
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence"/>
</property>
Also, jpaProperty should be jpaProperties

Related

Working with javax.persistence.sql-load-script-source in Spring Data JPA

I have read this question about schema data initialization for EclipseLink, and I want to achieve the same in Spring Data JPA.
The answers all assume the presence of a persistence.xml file, which I am not using.
Instead I have Spring config, as follows:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="packagesToScan" value="rbf.staffing.data"/>
<property name="jpaPropertyMap">
<map>
<entry key="eclipselink.weaving" value="false"/>
<entry key="eclipselink.id-validation" value="NULL"/>
<entry key="eclipselink.ddl-generation" value="create-tables" />
<entry key="javax.persistence.sql-load-script-source" value="main/resources/import.sql" />
</map>
</property>
</bean>
Where jpaVendorAdapter is a EclipseLinkJpaVendorAdapter.
All the DDL works great, but the import.sql is not being executed. It is in src/main/resources

what is the use of durability in quartz xml

Here a property mention that durability=true.
<bean name="complexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.websystique.spring.quartz.ScheduledJob" />
<property name="jobDataMap">
<map>
<entry key="anotherBean" value-ref="anotherBean1" />
<entry key="myBean" value-ref="myBean" />
</map>
</property>
<property name="durability" value="true" />
</bean>
could you please explain what is the use of durability=true.
From here:
Specify the job's durability, i.e. whether it should remain stored in
the job store even if no triggers point to it anymore.

Configuring Spring + Hibernate JPA Transaction Manager through JTA

I previously had this config for Hibernate using RESOURCE-LOCAL transaction type:
persistence.xml:
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
</persistence-unit>
applicationContext (dataaccess bit):
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"></bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="persistenceUnitName" value="myPU"/>
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<!-- Are there any other properties required? -->
</bean>
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/CNTXESDB" />
<property name="lookupOnStartup" value="true" />
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
But this kind of transaction seems not to work with Glassfish, so I had to switch to JTA transactions.
The problem is -- to get Spring to manage transaction creation (through #Transactional) I need to define a TransactionManager bean but JtaTransactionManager included in spring-tx does not accept an entityManagerFactory bean, so it does not know where the entityManager is in order to open/close/flush Hibernate session.
So how can I configure Spring with Hibernate to use JTA transactions?
EDIT:
turns out you can use RESOURCE_LOCAL transactions with Glassfish, but somehow you cannot have a persistence.xml file. I renamed this file to my_persistence.xml and configured LocalContainerEntityManagerFactoryBean like this:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="persistenceUnitName" value="myPU"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/my_persistence.xml" />
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
I had a similar problem and finally I solved as you can see in this little demo:
https://github.com/miguelangelprogramacion/spring4_jpa_hibernate
With [1] as a reference, I prefer to use Spring's Transaction Support before JTA.
Also, I've used an annotation based approach.
[1] http://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/

Quartz Cron scheduler in Spring with jdbc store

I need to create Cron service in Spring, but I can not find enough info how to do it with jdbc store. I want Quartz to use my present connection to Datasource, my database is PostgreSql. I need to create use jdbc store, because task should be done even if server was down.
You can try something like this
<bean id="scheduler-JDBC" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" abstract="true">
<property name="dataSource" ref="myDataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
<property name="overwriteExistingJobs" value="true" />
<property name="quartzProperties">
<props>
<prop key="org.quartz.jobStore.isClustered">false</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
</props>
</property>
</bean>
<bean id="cronScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" parent="scheduler-JDBC">
<property name="startupDelay" value="10" />
<property name="autoStartup" value="true" />
<property name="applicationContextSchedulerContextKey" value="applicationContext"/>
<property name="triggers">
<list>
<ref bean="myTrigger" />
</list>
</property>
</bean>
Download Quartz distribution from http://quartz-scheduler.org/ and you will find the database script for needed tables in docs/dbTables.

How to set the name of the net.sf.ehcache.CacheManager for JMX monitoring?

I am using EhCache 1.4.0, Spring 3.0.5 in a web application deployed on Tomcat 6 using JRE 1.6. I am exposing via JMX the L2 cache management, like this:
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true" />
</bean>
<util:property-path id="hibernateCacheProvider" path="sessionFactory.settings.cacheProvider" />
<bean id="hibernateEhCacheManager" class="com.mycompany.spring.beans.factory.config.UnaccessibleFieldRetrievingFactoryBean">
<property name="targetObject" ref="hibernateCacheProvider" />
<property name="targetField" value="manager" />
<property name="makeInstanceFieldVisible" value="true" />
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<description>The cacheManager configuration.</description>
<property name="targetClass" value="net.sf.ehcache.management.ManagementService" />
<property name="staticMethod" value="net.sf.ehcache.management.ManagementService.registerMBeans" />
<property name="arguments">
<list>
<ref bean="hibernateEhCacheManager" />
<ref bean="mbeanServer" />
<value type="boolean">true</value>
<value type="boolean">true</value>
<value type="boolean">true</value>
<value type="boolean">true</value>
</list>
</property>
</bean>
<bean class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
<property name="server" ref="mbeanServer" />
<property name="beans">
<map>
<entry key="Hibernate:type=statistics,application=applicationOne">
<bean class="org.hibernate.jmx.StatisticsService">
<property name="statisticsEnabled" value="true" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="hbm.properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">applicationOne-web/ehcache.xml</prop>
<prop key="hibernate.cache.query_cache_factory">org.hibernate.cache.StandardQueryCacheFactory</prop>
</props>
</property>
</bean>
I have to allow to clear all the entries in the L2 cache by using the jmxterm tool, like this:
run --bean net.sf.ehcache:type=CacheManager,name=net.sf.ehcache.CacheManager#605df3c5 clearAll
I am aware of jconsole to determine the exact CacheManager, from the context, but I may not use it for some reasons I won't get into.
So far, so good, but suppose that my JVM (Tomcat server) has 2 applications deployed, both allowing JMX monitoring for EhCache. The names of these two MBeans will be:
net.sf.ehcache:type=CacheManager,name=net.sf.ehcache.CacheManager#605df3c5
net.sf.ehcache:type=CacheManager,name=net.sf.ehcache.CacheManager#49ff3459
As you can see they are not quite useful when trying to determine which cache to clear.
So my question is: is there any possibility to set the name of each CacheManager, in order to identify exactly which one to use to clear all the entries in the L2 cache ?
Thank you.
I know this was answered long ago, but I think it is easier just to set it in your ehcache configuration file (applicationOne-web/ehcache.xml).
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect" dynamicConfig="true" name="MY_CACHE_MANAGER_NAME">
...
</ehcache>
Once the hibernateEhCacheManager is available one can invoke its methods (setting the including) using the following bean definition. Normally this should do the trick renaming the CacheManager.
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="hibernateEhCacheManager"/>
</property>
<property name="targetMethod">
<value>setName</value>
</property>
<property name="arguments" value="<the_desired_name>"/>
</bean>

Categories