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

后端 未结 2 1317
深忆病人
深忆病人 2021-02-05 09:11

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 successful

相关标签:
2条回答
  • 2021-02-05 09:46

    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);
    
    0 讨论(0)
  • 2021-02-05 09:49

    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>
    
    0 讨论(0)
提交回复
热议问题