title: Setting JPA persistence properties dynamically
body: <p>Let's say I have the following persistence.xml with the connection url, user &amp; password all hard-coded. </p>
  
  <p>The following is for Hibernate 3.2. For Hibernate 3.5 ++, we have to change "hibernate.connection" to "javax.persistence". But let me ask this question regardless of the literals "hibernate.connection" or "javax.persistence".</p>
  
  <pre><code>&lt;persistence-unit name="obamacare" transaction-type="RESOURCE_LOCAL"&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
    &lt;properties&gt;
      &lt;property name="hibernate.archive.autodetection" value="class, hbm"/&gt;
      &lt;property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/&gt;
      &lt;property name="hibernate.connection.url" value="blah blah blah"/&gt;
      &lt;property name="hibernate.connection.username" value="careuser"/&gt;
      &lt;property name="hibernate.connection.password" value="carepass"/&gt;
      &lt;property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/&gt;
      &lt;property name="hibernate.show_sql" value="true"/&gt;
    &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>However, we need to set the url, user &amp; password dynamically. There is a proposed authentication server which serves the url, user &amp; password. So that we do not need to individually configure the myriads of webapps that use some form of jdbc, hibernate or JPA. Besides the security issue of not wanting to store/manage passwords on visible text files.</p>
  
  <p>As far as JPA is concerned, how can I set these JPA properties dynamically? I am seeking two sets of answers:</p>
  
  <ol>
  <li><p>For a solution that is JPA vendor independent (toplink, eclipselink, hibernate, etc) - Is there any JPA functionality that would let me set these three properties dynamically?</p></li>
  <li><p>If I am allowed to depend totally on Hibernate, besides the possible JPA only avenue, is there a way to achieving this without involving Spring framework (which seems like a huge monstrosity with tentacles all over the place)?</p></li>
  </ol>
  
  <p>I would be elated if you also wish to throw in two cents/quids/rupees on JNDI and how I could use it to replace the functionality of persistence.xml properties. However, that is not the priority of the question.</p>