java 如何在 Spring JPA Web Application 中设置一些 Hibernate 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12237003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set some Hibernate properties in Spring JPA Web Application?
提问by Jér?me Verstrynge
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:
我试图摆脱persistence.xml
Spring JPA web 应用程序中的典型文件。到目前为止,我已经成功地注入EntityManager
了以下内容:
@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:
我已经设法为数据源移动了我的 persistence.xml 的属性:
<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
但是如何设置上面剩下的两个休眠属性?谢谢
回答by axtavt
Spring provides a way to configure these options in provider-independent way using AbstractJpaVendorAdapter
(setDatabase()
and setGenerateDdl()
, though setGenerateDdl()
doesn't take DDL mode).
Spring 提供了一种使用AbstractJpaVendorAdapter
(setDatabase()
和setGenerateDdl()
,但setGenerateDdl()
不采用 DDL 模式) 以独立于提供者的方式配置这些选项的方法。
Alternatively, you can pass arbitrary properties to LocalContainerEntityManagerFactory
using setJpaProperties()
(or setJpaPropertyMap()
):
或者,您可以将任意属性传递给LocalContainerEntityManagerFactory
using setJpaProperties()
(或setJpaPropertyMap()
):
Properties props = new Properties();
props.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
props.put("hibernate.hbm2ddl.auto", "create");
factoryBean.setJpaProperties(props);
回答by Ashok maurya
This is the old question but might help someone who is using XML for configuration.
这是一个老问题,但可能对使用 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>