title: Making JPA work with Spring Framework on JBoss server
body: <p><strong>Basis</strong></p>
  
  <p>I am using Spring 2.5.6 and Hibernate 3.2.1 - because I have to deploy to a jboss 4.2.3 whose setup is not allowed to be disturbed (because the jboss server is built by a script, and the script is old but  no charter given to invest time to change it). So, no, cannot have private libraries for the app on JBoss.</p>
  
  <p>I am a JPA by annotation person, not a hibernate XML person. So I am not going to change DTOs and mappings to using hibernate XML. Having to regress to Hibernate 3.2.1 from Hibernate 3.6 was already too much for me. I have hibernate auto detect my entity classes, and I will not be changing that.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I wrote a webapp which ran wonderfully with JPA and JAX-RS in JBoss 4.2.3 dev env. Now I have to fit it into Spring 2.5.6 framework.</p>
  
  <p>So, I created the following <strong>ApplicationContext.xml</strong> ...</p>
  
  <pre><code>&lt;bean id="propertyPlaceHolder" class="zzz.common.spring.config.ConfigPlaceholderConfigurer" &gt;
      &lt;property name="location" value="file:C:/config/bootstrap.properties"/&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; 
    &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"&gt; 
    &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" &gt;
    &lt;property name="persistenceUnitName" value="obamacare"/&gt;    
  &lt;/bean&gt;
  </code></pre>
  
  <p>~</p>
  
  <p>And this is the <strong>persistence.xml</strong>. Originally the property values were hardcoded during dev. But to turn them into production, their values have to be made into spring ${references}. The ${references} is read from file:C:/config/bootstrap.properties, and zzz.common.spring.config.ConfigPlaceholderConfigurer needs to set the user and password thro an auth service.</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;!-- Scan for annotated classes and Hibernate mapping XML files --&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.archive.autodetection" value="class, hbm" /&gt;
  
          &lt;property name="hibernate.connection.driver_class"
              value="${zzz.common.sql.ConnectionPool.driverClass}" /&gt;
          &lt;property name="hibernate.connection.url"
              value="${zzz.common.sql.ConnectionPool.url}" /&gt;
  
          &lt;property name="hibernate.connection.username" value="${zzz.common.sql.ConnectionPool.user}"/&gt;
          &lt;property name="hibernate.connection.password" value="${zzz.common.sql.ConnectionPool.password}"/&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;
  </code></pre>
  
  <p>Obviously, the spring ${references} in the persistence XML is not working, because the entity manager is Null, because the The jdbc driver class is null. As well as the url and user and password.</p>
  
  <p>I placed the following lines into the JAX-RS response code, which , where debugger shows that entityManager is null.</p>
  
  <pre><code>@PersistenceContext
  private EntityManager entityManager;
  </code></pre>
  
  <p><strong>Questions</strong>:</p>
  
  <p><strong>Q1.</strong></p>
  
  <p>What do I need to do to make the spring ${references} get injected into my persistence.xml properties?</p>
  
  <p><strong>Q2.</strong></p>
  
  <p>I tried to transfer the persisitence.xml properties into a datasource in ApplicationContext.xml.</p>
  
  <pre><code>&lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" &gt;
      &lt;property name="persistenceUnitName" value="unit1"/&gt;    
  &lt;/bean&gt;
  
  &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
      &lt;property name="driverClassName" value="${zzz.common.sql.ConnectionPool.driverClass}"/&gt;
      &lt;property name="url" value="${zzz.common.sql.ConnectionPool.url}"/&gt;
      &lt;property name="username" value="${zzz.common.sql.ConnectionPool.user}"/&gt;
      &lt;property name="password" value="${zzz.common.sql.ConnectionPool.password}"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>But JBoss start-up failed because ... DriverManagerDataSource is not writable, no setDatasource method or setDatasource method not accessible, blah, blah.</p>
  
  <p>Why is the datasource not settable in DriverManagerDataSource? What do I need to do in an extension class to create a setDatasource method. What would I need to do inside that method?</p>
  
  <p>Coould you suggest an alternative jdbc datasource that will allow setDatasource?</p>
  
  <p><strong>Q3.</strong></p>
  
  <p>Is there anything else I need to do or will spring framework automagically find the PersistenceContext annotation at entitymanager and then attempt to instantiate it?</p>
  
  <p><strong>Q4.</strong></p>
  
  <p>What does the bean jpaTemplate do? Why do I need it?</p>
  
  <p><strong>Q5.</strong></p>
  
  <p>If I transferred everything to ApplicationContext.xml, do I still have to keep a dummy placeholding persistence.xml around?</p>
  
  <p>I need help. By either making DriverManagerDataSource work in ApplicationContext.xml or making ${references} work in persistence.xml.</p>
  
  <p>Thank you all.</p>
  
  <p>This is a follow up to the question I asked last week: <a href="https://stackoverflow.com/questions/11695291/setting-jpa-persistence-properties-dynamically">Setting JPA persistence properties dynamically</a>.</p>