I have this peristence.xml
that deploys on 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 :
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
?
I've tried a number of JPA properties to override jta-data-source
, with no success:
// 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.
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
.
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? Then you can do whatever you want in your plain java application.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.