Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority between properties in HibernateJPAVendorAdapter and JPAProperty

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

like image 499
Alejandro Cuervo Avatar asked Sep 09 '13 12:09

Alejandro Cuervo


2 Answers

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.

like image 149
M. Deinum Avatar answered Oct 19 '22 17:10

M. Deinum


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

like image 27
ikumen Avatar answered Oct 19 '22 19:10

ikumen