title: How to implement Spring's @Transactional Annotation using Hibernate, HikariCP and persistence.xml
body: <p>I need to implement Spring's @Transactional annotation but I'm not being able to do so.</p>
  
  <p>I had tried a lot of methods in the past 3 weeks but none of them worked.</p>
  
  <p>I'm also need to use EntityManager. To test if Spring's was working, I tried to inject the EntityManager using @PersistenceContext (I also tried using @PersistenceUnit and/with EntityManagerFactory) but always I got nullPointerException.</p>
  
  <pre><code> @PersistenceContext(unitName = "sistema")
   protected EntityManager entityManager;
  </code></pre>
  
  <p>Basically I need to know how to make spring's annotations to work and how to implement a Transaction manager using those technologies:</p>
  
  <p><strong>persitence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
               version="2.0"&gt;
      &lt;persistence-unit name="sistema" transaction-type="RESOURCE_LOCAL"&gt; 
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;com.sis.vo.Person&lt;/class&gt;
  
          &lt;properties&gt;
              &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="hibernate.connection.autocommit" value="false" /&gt;
  
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/&gt;
              &lt;property name="hibernate.enable_lazy_load_no_trans" value="false"/&gt;
              &lt;property name="hibernate.show_sql" value="false" /&gt;
              &lt;property name="hibernate.format_sql" value="false"/&gt;
  
              &lt;!-- do I need those? --&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost/myDatabase?autoReconnect=true&amp;amp;useSSL=false" /&gt;
              &lt;property name="hibernate.connection.username" value="myUser" /&gt;
              &lt;property name="hibernate.connection.password" value="myPass" /&gt;
  
          &lt;/properties&gt;
     &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>spring.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                      http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context-4.3.xsd
                      http://www.springframework.org/schema/mvc
                      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                      http://www.springframework.org/schema/jee
                      http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
                      http://www.springframework.org/schema/tx
                      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"&gt;
  
  
      &lt;context:annotation-config /&gt;
      &lt;mvc:annotation-driven /&gt;
  
       &lt;bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"&gt;
          &lt;property name="poolName" value="sisHikariCP" /&gt;
          &lt;property name="connectionTestQuery" value="SELECT 1" /&gt;
          &lt;property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" /&gt;
          &lt;property name="minimumIdle" value="3" /&gt;
          &lt;property name="maximumPoolSize" value="100" /&gt;
          &lt;property name="idleTimeout" value="740000" /&gt;
          &lt;property name="maxLifetime" value="1740000" /&gt;
          &lt;property name="leakDetectionThreshold" value="30000" /&gt;
          &lt;property name="dataSourceProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="url"&gt;jdbc:mysql://localhost/myDatabase?autoReconnect=true&amp;amp;useSSL=false&lt;/prop&gt;
                  &lt;prop key="user"&gt;myUser&lt;/prop&gt;
                  &lt;prop key="password"&gt;myPass&lt;/prop&gt;
  
                  &lt;prop key="prepStmtCacheSize"&gt;350&lt;/prop&gt;
                  &lt;prop key="prepStmtCacheSqlLimit"&gt;2048&lt;/prop&gt;
                  &lt;prop key="cachePrepStmts"&gt;true&lt;/prop&gt;
                  &lt;prop key="useServerPrepStmts"&gt;true&lt;/prop&gt;
                  &lt;prop key="useLocalSessionState"&gt;true&lt;/prop&gt;
                  &lt;prop key="useLocalTransactionState"&gt;true&lt;/prop&gt;
                  &lt;prop key="rewriteBatchedStatements"&gt;true&lt;/prop&gt;
                  &lt;prop key="cacheResultSetMetadata"&gt;true&lt;/prop&gt;
                  &lt;prop key="cacheResultSetMetadata"&gt;true&lt;/prop&gt;
                  &lt;prop key="cacheServerConfiguration"&gt;true&lt;/prop&gt;
                  &lt;prop key="elideSetAutoCommits"&gt;true&lt;/prop&gt;
                  &lt;prop key="maintainTimeStats"&gt;false&lt;/prop&gt; 
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"&gt;
          &lt;constructor-arg ref="hikariConfig" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="myJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaVendorAdapter" ref="myJpaVendorAdapter" /&gt;
  
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"  /&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0"&gt;
      &lt;display-name&gt;sis&lt;/display-name&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsf&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
      &lt;!--  SPRING  --&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Spring Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
                  &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;
                      /WEB-INF/spring.xml
              &lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>DAO</strong></p>
  
  <pre><code>@Repository
  public class GenericListDAO&lt;E&gt; {
  
      @PersistenceContext(unitName = "sistema")
      protected EntityManager entityManager;
  
      public EntityManager getEntityManager() {
          return entityManager;
      }
  
      public GenericListDAO() {
      }
  
      //rest of the code (persist, find, etc)
  }
  </code></pre>
  
  <p>Versions:</p>
  
  <ul>
  <li>Spring Version: 4.3.12</li>
  <li>Hibernate Version: 5.1.10.Final</li>
  <li>HikariCP Version: 2.7.3</li>
  <li>JDK Version: 1.8.0_121</li>
  <li>Tomcat version: 8.5.23</li>
  </ul>
  
  <p>Thank you!</p>