I've checked in google many hours without good results so i share my code. If you have a better solution i'm very interessed!
The problem is :
I've added a JPA datasource into jboss configuration and i've added some properties
<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>
My 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>
And my code to instanciate the EntityManager :
Persistence.createEntityManagerFactory("mainDatabase");
The problem is that properties hibernate.hbm2ddl.auto
and hibernate.show_sql
wasn't added to the EntityManager. But these properties exists on datasource when i get it with
final Context lInitCtx = new InitialContext();
final Object lEnvCtx = lInitCtx.lookup(lJNDIName);
final DataSource lWrapperDataSource = (DataSource ) lEnvCtx;
So my solution is :
I've get the JNDI name from EntityManager :
EntityManagerFactory lEntityManagerFactory = Persistence.createEntityManagerFactory("mainDatabase");
final Map<String, Object> lConnectionProperties = lResult.getProperties();
// Extract JNDI name
final String lJNDIName = (String ) lConnectionProperties.get("hibernate.connection.datasource");
I've get datasource with it
final Context lInitCtx = new InitialContext();
final Object lEnvCtx = lInitCtx.lookup(lJNDIName);
final DataSource lWrapperDataSource = (DataSource ) lEnvCtx;
I've extracted properties form this datasource. It's the most bad code of the solution :
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);
I've copied these properties into a map
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);
}
And i've re-create the EntityManager with theses properties
lEntityManagerFactory = Persistence.createEntityManagerFactory("mainDatabase", lPersistenceProperties);
Again : If you have a better solution i will very happy to not use reflexion and get private member, use Context, ....