title: Spring + JPA + Hibernate
body: <p>I am newbie in Spring. I am trying to setup simple web application with Spring 3.1.3 and JPA 2.0
  I have added all necessary libs to the WEB-INF/lib. There is no errors during the startup but entityManager in my DaoImpl file is null.
  So, this is my configuration:</p>
  
  <p>persistance.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
  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"&gt;
  
  &lt;persistence-unit name="fcmsServer" transaction-type="RESOURCE_LOCAL"&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>fcms-servlet.xml</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: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/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
  &lt;!-- the application context definition for the fcmsServer DispatcherServlet --&gt;
  
  &lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="fcmsServer" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaDialect"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /&gt;
      &lt;/property&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="persistenceUnitManager"&gt;
          &lt;bean
              class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
              &lt;property name="defaultDataSource" ref="dataSource" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
      &lt;property name="url" value="jdbc:h2:tcp://localhost/~/fcms" /&gt;
      &lt;property name="username" value="sa" /&gt;
      &lt;property name="password" value="sa" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="persistenceAnnotation"
  class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  
  &lt;bean id="userDao" class="fcms.data.user.UserDAOImpl"&gt;
  &lt;/bean&gt;
  
  &lt;bean name="/user.htm" class="fcms.controller.UserController"&gt;
  &lt;/bean&gt;
  
  &lt;bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="viewClass"
          value="org.springframework.web.servlet.view.JstlView"&gt;&lt;/property&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/"&gt;&lt;/property&gt;
      &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>DaoImpl class:</p>
  
  <pre><code>@Repository
  public class UserDAOImpl implements UserDAO {
  @PersistenceContext(unitName = "fcmsServer")
  private EntityManager entityManager;
  
  public void setEntityManager(EntityManager entityManager) {
      this.entityManager = entityManager;
  }
  
  @Override
  public User getUserById(long id) {
      return entityManager.find(User.class,id);
  }
  
  @Override
  public void addUser(User u) {
      entityManager.persist(u);
  }
  }
  </code></pre>
  
  <p>User class</p>
  
  <pre><code>@Entity
  @Table(name = "User")
  public class User implements Serializable {
  
  
  @Id  @GeneratedValue
  private long id;
  @Column(name = "lastName", nullable = false)
  private String lastName;
  @Column(name = "firstName", nullable = false)
  private String firstName;
  @Column(name = "birthDate", nullable = true)
  private Date birthDate;
  private static final long serialVersionUID = 1L;
  </code></pre>
  
  <p>
      }</p>
  
  <p>So when I try to call addUser() in UserController, I have NullPointerException during persist. </p>