2012-08-29 39 views
0

在使用Spring和Hibernate我们的Web应用程序,在Hibernate配置在META-INF/persistence.xml,但有一个问题,我们使用了两个不同的数据库,一个用来测试和另一个用于生产。Spring和Hibernate - 改变方言

这里是我们的`的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
    version="2.0"> 
    <persistence-unit name="SpringMVCTest" transaction-type="JTA"> 
<provider>org.hibernate.ejb.HibernatePersistence</provider> 
<jta-data-source>java:/comp/env/jdbc/sqliteDS</jta-data-source> 
     <class>pl.meble.taboret.model.UserEntity</class> 
     <class>pl.meble.taboret.model.WordList</class> 
     <class>pl.meble.taboret.model.WordUnit</class> 
     <class>pl.meble.taboret.model.ActivateUserAccountPermaLink</class> 
     <class>pl.meble.taboret.model.ResetPasswordPermaLink</class> 
     <class>pl.meble.taboret.question.QuestionUnit</class> 
     <class>pl.meble.taboret.question.OpenQuestion</class> 
     <class>pl.meble.taboret.question.MultipleChoiceQuestion</class> 
     <class>pl.meble.taboret.question.WithGapsQuestion</class> 
     <class>pl.meble.taboret.question.QuestionList</class> 
     <class>pl.meble.taboret.answer.AnswerUnit</class> 
     <class>pl.meble.taboret.answer.OpenQuestionAnswer</class> 
     <class>pl.meble.taboret.answer.MultipleChoiceQuestionAnswer</class> 
     <class>pl.meble.taboret.answer.WithGapsQuestionAnswer</class> 
     <exclude-unlisted-classes>false</exclude-unlisted-classes> 
     <properties> 
      <property name="hibernate.dialect" value="pl.meble.taboret.utils.SQLiteDialect" /> 
      <property name="hibernate.hbm2ddl.auto" value="update" /> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="hibernate.format_sql" value="true" /> 
      <property name="hibernate.use_sql_comments" value="true" /> 
      <!-- <property name="hibernate.connection.driver_class" value="${database.driver}" 
       /> <property name="hibernate.connection.url" value="${database.url}" /> --> 

     <!--  <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/> --> 
     <!--<property name="hibernate.transaction.factory_class" value="com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory"/> --> 
     <property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

因此,是否有可能在运行时更改的hibernate.properties值,或例如该值存储在JNDI资源?

或者是否有其他方式有条件地设置hibernate.dialect,所以例如测试我们将SQLite方言和正常部署他会使用Postgre方言。

+0

你可以在这里检查这个问题[点击这里](http: //stackoverflow.com/questions/33166150/hibernate-jta-read-db-connection-parameters-per-environment/33309337#33309337) –

回答

4

是。在春天,你定义实体管理一个bean:

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 

您可以配置bean的属性:

<property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
     </props> 
</property> 

其中${hibernate.dialect}是弹簧性能。所以你可以在开始你的项目时通过属性(通过-Dhibernate.dialect,或者把它放在一个属性文件中并加载它)<context:property-placeholder-configurer>

+0

它的工作原理我想,但我有一个问题,你的片断工作,但是当我尝试'属性名= “hibernate.dialect” 值= “pl.meble.taboret.utils.SQLiteDialect”/>'我正在bean类的'无效的属性 '冬眠'[org.springframework.orm.jpa.LocalEntityManagerFactoryBean]'。有什么区别两者都应设置属性 – Andna

+0

都能跟得上 - ?'<属性名= ..'是bean本身的属性(与设定器/吸气剂字段)的JPA性能列于'jpaProperties',这是一个设置。在“地图”中(工厂)bean – Bozho

+0

好的,谢谢,我只是争辩说jpaProperties的名字没有任何意义。 – Andna