Spring和Hibernate - 改变方言 [英] Spring and Hibernate - changing dialect

查看:77
本文介绍了Spring和Hibernate - 改变方言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Spring和Hibernate的web应用程序中,hibernate配置位于 META-INF / persistence.xml 中,但有一个问题,我们使用两个不同的数据库,一个用于测试,另一个用于生产。

In our web app that uses Spring and Hibernate, the hibernate configuration is in the META-INF/persistence.xml, but there is one problem, we are using two different databases, one for testing and other one for production.

这是我们的`persistence.xml

Here is our `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 资源中?

so, is it possible to change the value of hibernate.properties at runtime, or store this value for example in JNDI resource?

另一种有条件地设置 hibernate.dialect 的方法,例如为了测试我们会有SQLite方言,并且对于正常部署,他会使用Postgre方言。

Or is there some other way to conditionally set hibernate.dialect, so for example for testing we would have SQLite dialect and for normal deploy he would use Postgre dialect.

推荐答案

是的。在春季你用一个bean定义实体管理器:

Yes. In spring you define the entity manager with a bean:

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

您可以配置该bean的属性:

You can configure properties of that bean:

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

其中 $ {hibernate.dialect} 是泉水属性。因此,您可以在启动项目时传递属性(通过-Dhibernate.dialect或通过将其放置在属性文件中并使用< context:property-placeholder-configurer>

where ${hibernate.dialect} is spring property. So you can pass the property when starting your project (either via -Dhibernate.dialect, or by placing it in a properties file and loading it with <context:property-placeholder-configurer>

这篇关于Spring和Hibernate - 改变方言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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