Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set some Hibernate properties in Spring JPA Web Application?

I am trying to get rid of the typical persistence.xml file in Spring JPA web application. So far, I have managed to inject the EntityManager successfully with the following:

@Configuration
@EnableTransactionManagement
public class JpaConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){

        LocalContainerEntityManagerFactoryBean factoryBean
            = new LocalContainerEntityManagerFactoryBean();

        factoryBean.setDataSource( this.restDataSource() );
        factoryBean.setPackagesToScan( new String[ ] { "com.jverstry" } );
        factoryBean.setPersistenceUnitName("MyMy");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(){
         {
            // JPA properties ...
         }
        };

        factoryBean.setJpaVendorAdapter( vendorAdapter );

        return factoryBean;

    }

    @Bean
    public DataSource restDataSource(){

        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
        dataSource.setUrl("jdbc:hsqldb:mem:testdb");
        dataSource.setUsername("sa");
        dataSource.setPassword("");

        return dataSource;

    }

    @Bean
    public PlatformTransactionManager transactionManager(){

        JpaTransactionManager transactionManager = new JpaTransactionManager();

        transactionManager.setEntityManagerFactory(
            this.entityManagerFactoryBean().getObject() );

        return transactionManager;

    }

}

I have managed to move the properties of my persistence.xml for the datasource:

<properties>
    ... 
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    <property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>

but how to I set the two remaining hibernate properties above? Thanks

like image 898
Jérôme Verstrynge Avatar asked Sep 02 '12 15:09

Jérôme Verstrynge


People also ask

What is the use of spring JPA properties Hibernate Dialect?

It is the default JPA vendor that comes with spring-data-jpa. Although Hibernate is database agnostic, we can specify the current database dialect to let it generate better SQL queries for that database. The ddl-auto property is used to automatically create the tables based on the entity classes in the application.


2 Answers

Spring provides a way to configure these options in provider-independent way using AbstractJpaVendorAdapter (setDatabase() and setGenerateDdl(), though setGenerateDdl() doesn't take DDL mode).

Alternatively, you can pass arbitrary properties to LocalContainerEntityManagerFactory using setJpaProperties() (or setJpaPropertyMap()):

Properties props = new Properties();
props.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
props.put("hibernate.hbm2ddl.auto", "create");
factoryBean.setJpaProperties(props);
like image 128
axtavt Avatar answered Oct 01 '22 11:10

axtavt


This is the old question but might help someone who is using XML for configuration.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="test-jpa"/>
    <property name="dataSource" ref="dataSourceProxy"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="database" value="MYSQL"/>
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.jdbc.batch_size" value="10"/>
            <entry key="hibernate.jdbc.fetch_size" value="10"/>
            <entry key="hibernate.order_inserts" value="true"/>
            <entry key="hibernate.order_updates" value="true"/>
            <entry key="hibernate.jdbc.batch_versioned_data" value="true"/>
            <entry key="hibernate.format_sql" value="true"/>
        </map>
    </property>
</bean>
like image 35
Ashok maurya Avatar answered Oct 01 '22 11:10

Ashok maurya