【发布时间】:2015-08-09 03:35:35
【问题描述】:
我已经在 google 中检查了很多小时,但没有得到很好的结果,所以我分享了我的代码。 如果您有更好的解决方案,我很感兴趣!
问题是:
我已经在 jboss 配置中添加了一个 JPA 数据源,并且我已经添加了一些属性
<datasource jta="false" jndi-name="java:/jdbc/MyApp" pool-name="MyApp_main" enabled="true" use-ccm="false">
<connection-url>jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS test</connection-url>
<driver-class>org.h2.Driver</driver-class>
<connection-property name="hibernate.hbm2ddl.auto">
create-drop
</connection-property>
<connection-property name="hibernate.show_sql">
true
</connection-property>
<driver>h2</driver>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>0</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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 ">
<persistence-unit name="mainDatabase"
transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:/jdbc/MyApp</jta-data-source>
<class>org.company.app.business.class1</class>
<class>org.company.app.business.class2</class>
<class>org.company.app.business.class3</class>
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
</persistence>
还有我的 EntityManager 实例化代码:
Persistence.createEntityManagerFactory("mainDatabase");
问题是属性hibernate.hbm2ddl.auto 和hibernate.show_sql 没有添加到EntityManager。但是当我得到它时,这些属性存在于数据源上
final Context lInitCtx = new InitialContext();
final Object lEnvCtx = lInitCtx.lookup(lJNDIName);
final DataSource lWrapperDataSource = (DataSource ) lEnvCtx;
所以我的解决方案是:
我从 EntityManager 获得了 JNDI 名称:
EntityManagerFactory lEntityManagerFactory = Persistence.createEntityManagerFactory("mainDatabase");
final Map<String, Object> lConnectionProperties = lResult.getProperties();
// Extract JNDI name
final String lJNDIName = (String ) lConnectionProperties.get("hibernate.connection.datasource");
我已经得到了数据源
final Context lInitCtx = new InitialContext();
final Object lEnvCtx = lInitCtx.lookup(lJNDIName);
final DataSource lWrapperDataSource = (DataSource ) lEnvCtx;
我已经从这个数据源中提取了属性。 这是解决方案中最糟糕的代码:
final Connection lConnection = lWrapperDataSource.getConnection();
final Field lField = lWrapperDataSource.getClass().getDeclaredField("mcf");
lField.setAccessible(true);
final Object lMCF = lField.get(lWrapperDataSource);
final Field lConnectionProps = lMCF.getClass().getDeclaredField("connectionProps");
lConnectionProps.setAccessible(true);
final Properties lProperties = (Properties ) lConnectionProps.get(lMCF);
我已将这些属性复制到地图中
final Map<String, String> lPersistenceProperties = new HashMap<>();
for (final Entry<Object, Object> lEntry : lProperties.entrySet()) {
final String lKey = (String ) lEntry.getKey();
final String lValue = (String ) lEntry.getValue();
lPersistenceProperties.put(lKey, lValue);
}
我已经用这些属性重新创建了 EntityManager
lEntityManagerFactory = Persistence.createEntityManagerFactory("mainDatabase", lPersistenceProperties);
再次:如果您有更好的解决方案,我将很高兴不使用反射并获取私有成员,使用上下文,....
【问题讨论】:
-
我迷路了——你为什么不将这两个属性添加到你的 persistence.xml 中?您已经在那里声明了一个休眠特定属性(自动检测),只需添加这两个。
-
我想提供使用 Oracle 或 H2 数据库的可能性。在我的公司中,如果我们使用 Oracle,我们必须外部化用于数据库创建/演进的脚本。但如果它是 h2-memory 我必须直接通过我的 web 应用程序来完成。
-
我自己为此使用了两个不同的持久性单元。
-
Gimby > 你有 persistence-h2.xml /persistence-oracle.xml 你对用户说“将它重命名为你的战争”不是吗?我不想让用户修改战争,因为安装是自动的。
标签: java hibernate jpa jboss entitymanager