9

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

Alejandro Cuervo
  • 535
  • 1
  • 5
  • 16

2 Answers2

5

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
2

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

ikumen
  • 11,275
  • 4
  • 41
  • 41
  • Thanks for the answer!! Especifying HibernateJPAVendorAdapter wouldn´t it be enought? – Alejandro Cuervo Sep 09 '13 at 14:23
  • I'm just saying if you were to set `jpaProperties` alone without setting `jpaVendorAdaptor`, then you'd need to set `persistenceProvider`. Specifying a default jpaVendorAdaptor (ie. one without properties) may also be enough, although I've never tried it. – ikumen Sep 09 '13 at 15:26
  • Fine!! I didn't undestand well. Now I did!! Thanks – Alejandro Cuervo Sep 09 '13 at 15:51