我使用了一种适用于休眠的方法。
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>
就是这样。