【问题标题】:How to externalize properties of persistence.xml for JBOSS 7.1.1?如何为 JBOSS 7.1.1 外部化 persistence.xml 的属性?
【发布时间】:2014-04-30 09:28:25
【问题描述】:

我的 persistence.xml 当前存在于应用程序战争中(在 META-INF 文件夹中。)。但是,对于要跨多个 DB 运行的应用程序,需要一次又一次地更改持久性。我想避免它。但是,我无法理解我将如何从我将根据我的数据库更改的属性文件配置persistence.xml 中的属性(如方言),因此不会强迫我更新和重新部署我的战争。

如果我可以在我提到其他数据库详细信息的standalone.xml 中的数据源中配置方言,我的问题也可以解决。我无法弄清楚该物业将是什么。 虽然我更喜欢第一个解决方案。

PS:我是 Web App 开发的新手。问题可能会惹恼你。 :D

【问题讨论】:

    标签: jboss7.x datasource persistence.xml


    【解决方案1】:

    我使用了一种适用于休眠的方法。

    1) 将休眠配置属性放在一个 xml 文件中(称为 hibernate.cfg.xml,但不是强制性的)

    这是一个例子:

    <?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>
    

    你可以只放不以hibernate.ejb开头的hibernate属性

    2) 创建一个 jboss 模块。这很简单。假设您要调用模块 com.myorganization.config,而不是在服务器安装的 modules 文件夹中创建目录结构:/com/myorganization/config/main。在主文件夹中放置 hibernate.cfg.xml 文件和以下 module.xml 文件:

    <?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) 在您的 persistence.xml 添加以下属性:

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

    4) 最后,在 META-INF/MANIFEST.MF 文件中添加以下行:

    Dependencies: com.myorganization.config
    

    如果你喜欢 maven,请使用 maven-war-plugin 来更改 MANIFEST.MF:

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

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      相关资源
      最近更新 更多