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