在我们使用 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 方言。