I have a JavaEE application (WildFly 8.2 application server, JPA 2.1, Hibernate 4.3...)
As I use JSF I'm using the JSF project stage (wich value is Development
or SystemTest
or Production
according to if we are in dev or in the test or prod server)
We configure the JSF project stage via JNDI within WildFly combining this JBoss how to with the WlidFly specific way so the lookup of "java:/env/jsf/ProjectStage"
return Development
or SystemTest
or Production
depending on the environment
For instance this allow us to configure the log without changing the source code (we are using SLF4J and logback) and the logback configuration file look like :
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<insertFromJNDI env-entry-name="java:/env/jsf/ProjectStage" as="projectStage" />
<property name="STAGE" value="${projectStage}"/>
<if condition='property("STAGE").equals("Production")'>
<then>
<property name="LEVEL" value="INFO"/>
</then>
<else>
<if condition='property("STAGE").equals("SystemTest")'>
<then>
<property name="LEVEL" value="INFO"/>
</then>
<else>
<property name="LEVEL" value="TRACE"/>
</else>
</if>
</else>
</if>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="DEBUG" level="TRACE"/>
<root level="${LEVEL}">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
I show the logback conf file because I would like to do such a thing with the JPA persistence.xml
conf file
What I would like to configure is the hibernate.show_sql
in the persistence.xml
<persistence ...>
<persistence-unit name="myappPU" transaction-type="JTA">
<jta-data-source>java:/jdbc/myappDS</jta-data-source>
<!-- ... -->
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- ... -->
</properties>
</persistence-unit>
</persistence>
How to configure JPA/Hibernate within the persistence.xml
(such as logback.xml
)?
If it's not possible how to configure JPA/Hibernate programmatically in a JavaEE way? we use JPA doing:
@PersistenceContext(unitName = "myappPU", type = PersistenceContextType.EXTENDED)
private EntityManager value ;
I think about to configure the EntityManagerFactory
which as a Map<String,Object> getProperties()
method but the javadoc says : "Changing the contents of the map does not change the configuration" so how to do?
EDIT
Thank you @Nayan Wadekar and @Martijn
without testing the solution of @Nayan Wadekar, I think that calling Persistence.createEntityManagerFactory(unitName, map)
return a new EntityManagerFactory
but doesn't change the EntityManagerFactory
used by WildFly, so that won't impact injected entityManager
The solution of @Martijn Burger is very instructive, I learned a lot about CDI but, if I well understand the solution, it make me edit the beans.xml
CDI conf file instead of the persistence.xml
JPA conf file and I don't find out how to configure beans.xml
with JNDI such as logback.xml
in order to deploy my war without editing anything...
But those two responses gave me some clues :
- maybe I can change programmatically the
EntityManagerFactory
WildFly is using via JNDI - maybe I could write a CDI producer method to inject
EntityManager
in the app
I will try...
Those responses make me think I was not enough precise: my goal is to deploy the same war
in dev, prod or test without editing anything even conf file (but may be it is a not so good idea...)