[英]JPA: Reuse persistence.xml with jta-data-source in JSE and JUnit by overriding the datasource
I have this peristence.xml
that deploys on WildFly: 我有这个
peristence.xml
上WildFly部署:
<persistence-unit name="optaweb-employee-rostering-persistence-unit" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
...
</persistence-unit>
Now I'd like to reuse it in a plain Java application, with a direct JDBC connection, so without JNDI : 现在,我想在具有直接JDBC连接的纯Java应用程序中重用它,因此没有JNDI :
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.driver", "org.hsqldb.jdbcDriver");
properties.put("javax.persistence.jdbc.url", "jdbc:hsqldb:mem:testdb");
properties.put("javax.persistence.jdbc.user", "sa");
properties.put("javax.persistence.jdbc.password", "");
// Overwrites transaction-type successfully
properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
// TODO overwrite jta-data-source
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(
"optaweb-employee-rostering-persistence-unit", properties);
How do I overwrite jta-data-source
? 如何覆盖
jta-data-source
?
I've tried a number of JPA properties to override jta-data-source
, with no success: 我尝试了许多JPA属性来覆盖
jta-data-source
,但没有成功:
// Overwrites jta-data-source
// but triggers a JNDI lookup of "" which crashes of course
properties.put("javax.persistence.jtaDataSource", "");
// Does not overwrite jta-data-source
properties.put("javax.persistence.jtaDataSource", null);
// Does not overwrite jta-data-source
properties.put("javax.persistence.nonJtaDataSource", "foo");
I've also tried a number of hibernate specific properties, such as hibernate.transaction.coordinator_class
and hibernate.connection.datasource
with the same failing results as above. 我还尝试了许多特定于休眠的属性,例如
hibernate.transaction.coordinator_class
和hibernate.connection.datasource
,它们的失败结果与上述相同。
As far as I can tell from the source of Hibernate ORM (in particular org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl#EntityManagerFactoryBuilderImpl(org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor, java.util.Map, java.lang.ClassLoader, org.hibernate.boot.registry.classloading.spi.ClassLoaderService)
), these particular JPA settings override settings from hibernate.properties
or from the Map you will provide to Persistence.createEntityManagerFactory
. 据我从Hibernate ORM(尤其是
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl#EntityManagerFactoryBuilderImpl(org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor, java.util.Map, java.lang.ClassLoader, org.hibernate.boot.registry.classloading.spi.ClassLoaderService)
),这些特定的JPA设置会覆盖hibernate.properties
或您将提供给Persistence.createEntityManagerFactory
的Map中的设置。
It may be a dumb idea, but can't you just do the opposite, ie not set the datasource in your persistence unit, but set it through a hibernate.properties
file in your WildFly application? 这可能是一个愚蠢的主意,但是您是否不能做相反的事情,即不在持久性单元中设置数据源,而是通过WildFly应用程序中的
hibernate.properties
文件设置它呢? Then you can do whatever you want in your plain java application. 然后,您可以在普通的Java应用程序中执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.