title: Why did I need to define a Hibernate-JPA vendor adapter in my Spring application context?
body: <p>I spent days and days trying to get a working database connection for my Spring/JPA(Hibernate) integration tests, troubleshooting mysterious "No Database Context Found" errors, and I finally got it working, but I don't understand why I had to do what I did.</p>
  
  <p>Note how my <em>LocalContainerEntityManagerFacotryBean</em> references  a <em>HibernateJpaVendorAdapter</em>.</p>
  
  <h2>applicationContext.xml</h2>
  
  <pre><code>&lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="myEMF"&gt;
      &lt;property name="persistenceXmlLocation" value="file:src/test/resources/META-INF/persistence.xml" /&gt;
      &lt;property name="persistenceUnitName" value="myPU" /&gt;
      &lt;property name="jpaVendorAdapter" ref="hibernateJpaAdapter" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateJpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
      &lt;property name="showSql" value="true" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Why did I have to use this <em>HibernateJpaVendorAdapter</em> when my persistence unit is already configured for Hibernate as shown below?</p>
  
  <h2>persistence.xml</h2>
  
  <pre><code>&lt;persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;class&gt;com.blah.blah.Class1&lt;/class&gt;
      &lt;class&gt;com.blah.blah.Class2&lt;/class&gt;
      &lt;class&gt;com.blah.blah.Class3&lt;/class&gt; 
      &lt;properties&gt;
          &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1?zeroDateTimeBehavior=convertToNull"/&gt;
          &lt;property name="hibernate.connection.username" value="uname"/&gt;
          &lt;property name="hibernate.connection.password" value="pwd"/&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" /&gt;
  
          &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
          &lt;property name="hibernate.show_sql" value="true" /&gt;
          &lt;property name="hibernate.ejb.event.post-insert"
          value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.post-update"
          value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.post-delete"
          value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.pre-collection-update"
          value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.pre-collection-remove"
          value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.post-collection-recreate"
          value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>It seems to work, but am I doing this right?  Could I have done it another/better way?</p>