Question

My persistence.xml is currently present in the application war(in META-INF folder.). However, for the application to be run across multiple DBs the persistence needs to be changed again and again. I want to avoid it. But, I am not able to understand how will i configure the properties(like dialect) in the persistence.xml from, say, a property file which i would change based on my DB, hence not compelling me to update and redeploy my war.

My problem can also be resolved if i can configure the dialect in the datasource in standalone.xml where i have mentioned other DB details. I am not being able to make out what the property would be. Though i would prefer a solution for the first one.

PS: I am rookie in Web App development. Questions might annoy you. :D

Was it helpful?

Solution

I use a method that works well for hibernate.

1) put the hibernate configuration properties in a xml file (call it hibernate.cfg.xml but it's not mandatory)

this is an example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.search.default.directory_provider">ram</property>
    </session-factory>
</hibernate-configuration>

you can put there only hibernate properties that do not start with hibernate.ejb

2) Create a jboss module. It's very simple. Suppose you want to call the module com.myorganization.config than create a directory structure in the modules folder of you server installation: /com/myorganization/config/main. In the main folder put the hibernate.cfg.xml file and the following module.xml file:

<?xml version="1.0" encoding="UTF-8"?>  
<module xmlns="urn:jboss:module:1.1" name="com.myorganization.config">  
    <resources>  
        <resource-root path="."/>  
    </resources>
</module>

3) In your persistence.xml adding the following property:

<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />

4) Finally, in the META-INF/MANIFEST.MF file add the following line:

Dependencies: com.myorganization.config

If you like maven use the maven-war-plugin in order to change the MANIFEST.MF:

<configuration>
    <archive>
        <manifestEntries>
            <Dependencies>com.myorganization.config</Dependencies>
        </manifestEntries>
    </archive>
</configuration>

That's all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top