如何在Spring JPA Web应用程序中设置一些Hibernate属性?

2022-09-02 09:42:47

我正在尝试摆脱Spring JPA Web应用程序中的典型文件。到目前为止,我已经设法成功地注入了以下内容:persistence.xmlEntityManager

@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;

    }

}

我已经设法移动了数据源的持久性.xml的属性:

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

但是我如何设置上面剩下的两个休眠属性?谢谢


答案 1

Spring 提供了一种使用 ( 和 , 虽然不采用 DDL 模式) 以独立于提供程序的方式配置这些选项的方法。AbstractJpaVendorAdaptersetDatabase()setGenerateDdl()setGenerateDdl()

或者,您可以使用 (或):LocalContainerEntityManagerFactorysetJpaProperties()setJpaPropertyMap()

Properties props = new Properties();
props.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
props.put("hibernate.hbm2ddl.auto", "create");
factoryBean.setJpaProperties(props);

答案 2

这是一个老问题,但可能会帮助使用 XML 进行配置的人。

<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>

推荐