如何在Spring JPA Web应用程序中设置一些Hibernate属性? [英] How to set some Hibernate properties in Spring JPA Web Application?

查看:109
本文介绍了如何在Spring JPA Web应用程序中设置一些Hibernate属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图摆脱Spring JPA Web应用程序中的典型persistence.xml文件.到目前为止,我已经成功通过以下命令成功注入了EntityManager:

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;

    }

}

我设法移动了persistence.xml的属性作为数据源:

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

推荐答案

Spring提供了一种使用AbstractJpaVendorAdapter(setDatabase()setGenerateDdl(),但setGenerateDdl()并不这样)以与提供者无关的方式配置这些选项的方法.采取DDL模式).

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

或者,您可以使用setJpaProperties()(或setJpaPropertyMap())将任意属性传递给LocalContainerEntityManagerFactory:

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

这篇关于如何在Spring JPA Web应用程序中设置一些Hibernate属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆