Andna
Andna

Reputation: 6689

Spring and Hibernate - changing dialect

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.

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> 

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

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.

Upvotes: 0

Views: 3660

Answers (1)

Bozho
Bozho

Reputation: 597076

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

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

You can configure properties of that bean:

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

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>

Upvotes: 4

Related Questions