How to externalize properties of persistence.xml for JBOSS 7.1.1?

筅森魡賤 提交于 2019-12-06 16:28:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!