title: Spring mvc + hibernate/jpa -> entity manager is not injected despite @PersistenceContext
body: <p>I have a problem with EntityManager. When I try to use EntityManager in  a dao class, I got null pointer exception. So EntityManager is not injected despite @PersistenceContext annotation.</p>
  
  <p>My dao:</p>
  
  <pre><code>package com.fido.pia.dao;
  
  import com.fido.pia.model.User;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public class UserDao {
  
     @PersistenceContext
      protected EntityManager entityManager;
  
      public User save(User row) {
          if(row.isNew()) {
              entityManager.persist(row);
              return row;
          } else {
              return entityManager.merge(row);
          }
      }
  }
  </code></pre>
  
  <p>Servlet Config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans  xmlns="http://www.springframework.org/schema/beans"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:p="http://www.springframework.org/schema/p"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;!-- 
      Adds some default beans (HandlerAdapter, HandlerMapping, Binding Initializer...). It also turn on some annotations. 
      Explanation in http://stackoverflow.com/questions/28851306/spring-framework-what-is-the-purpose-of-mvcannotation-driven
  
      WITHOUT THIS, @RequestMapping ANNOTATIONS ARE LOADED, BUT MAPPING DO NOT WORK!!
      --&gt;
      &lt;mvc:annotation-driven /&gt;
  
  &lt;!--    Set loading annotations from classes
      &lt;context:component-scan base-package="com.fido.pia"/&gt;--&gt;
  
      &lt;!--manual homepage--&gt;
      &lt;mvc:view-controller path="/" view-name="home"/&gt;
  
      &lt;!--view resolver--&gt;
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/view/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;!--static resources - request will be handeled by ResourceHttpRequestHandler--&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!--database config--&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/pia" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
      &lt;!--entity manager factory--&gt;
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="packagesToScan" value="com.fido.pia" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;!--&lt;property name="generateDdl" value="true" /&gt;--&gt;
                  &lt;property name="showSql" value="true" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transactions --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- enable the configuration of transactional behavior based on annotations --&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;!--Set loading annotations from classes--&gt;
      &lt;context:component-scan base-package="com.fido.pia"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Any ideas what is wrong here?</p>