0

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

这是我们的`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 方言。

4

1 回答 1

4

是的。在 spring 中,您使用 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}spring 属性在哪里。因此,您可以在启动项目时传递该属性(通过 -Dhibernate.dialect 或将其放置在属性文件中并使用<context:property-placeholder-configurer>

于 2012-08-29T12:52:23.453 回答