title: Hibernate: How configure EntityManager in Hibernate?
body: <p>I create a hibernate project with 'hibernate tools'provide by JBoss to Eclipse.
  Generated the Entities (POJO's) and then the DAO's.</p>
  
  <p>This way for example:</p>
  
  <pre><code>@Entity
  @Table(name = "area", catalog = "project_schema", uniqueConstraints = @UniqueConstraint(columnNames = "area"))
  public class Area implements java.io.Serializable {
  
      private Integer id;
      private String area;
  
      public Area() {
      }
  
      public Area(String area) {
          this.area = area;
      }
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "id", unique = true, nullable = false)
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      @Column(name = "area", unique = true, nullable = false, length = 45)
      public String getArea() {
          return this.area;
      }
  
      public void setArea(String area) {
          this.area = area;
      }
  
  }
  </code></pre>
  
  <p>And then the respectely DAO class (generated by Hibernate Tools too):</p>
  
  <pre><code>@Stateless
  public class AreaHome {
  
      private static final Log log = LogFactory.getLog(AreaHome.class);
  
      @PersistenceContext
      private EntityManager entityManager;
  
      public void persist(Area transientInstance) {
          log.debug("persisting Area instance");
          try {
              entityManager.persist(transientInstance);
              log.debug("persist successful");
          } catch (RuntimeException re) {
              log.error("persist failed", re);
              throw re;
          }
      }
  
      public void remove(Area persistentInstance) {
          log.debug("removing Area instance");
          try {
              entityManager.remove(persistentInstance);
              log.debug("remove successful");
          } catch (RuntimeException re) {
              log.error("remove failed", re);
              throw re;
          }
      }
  
      public Area merge(Area detachedInstance) {
          log.debug("merging Area instance");
          try {
              Area result = entityManager.merge(detachedInstance);
              log.debug("merge successful");
              return result;
          } catch (RuntimeException re) {
              log.error("merge failed", re);
              throw re;
          }
      }
  
      public Area findById(Integer id) {
          log.debug("getting Area instance with id: " + id);
          try {
              Area instance = entityManager.find(Area.class, id);
              log.debug("get successful");
              return instance;
          } catch (RuntimeException re) {
              log.error("get failed", re);
              throw re;
          }
      }
  }
  </code></pre>
  
  <p>But when I try to call <code>AreaHome.persist()</code> it launchs an exception <code>'NullPointerException'</code>.</p>
  
  <p>I configure my project with hibernate.cfg.xml and everything works fine though:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;&lt;password&gt;&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://&lt;hostname&gt;:3306/&lt;schema&gt;&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
          &lt;!-- &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt; --&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
          &lt;!-- SQL --&gt;
          &lt;property name="hibernate.format_sql"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
          &lt;!-- C3P0 --&gt;
          &lt;property name="hibernate.c3p0.acquire_increment"&gt;2&lt;/property&gt;
          &lt;property name="hibernate.c3p0.max_size"&gt;20&lt;/property&gt;
          &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
          &lt;property name="hibernate.c3p0.timeout"&gt;180&lt;/property&gt;
          &lt;property name="hibernate.c3p0.idle_test_period"&gt;100&lt;/property&gt;
          &lt;!-- Classes --&gt;
          &lt;mapping class="com.suaparte.pojo.Area" /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>This works fine when I try:</p>
  
  <pre><code>public void persist(Area area) throws ExceptionHandler {
      Session session = HibernateUtil.getSessionFactory().openSession();
      try {
          session.beginTransaction();
          session.save(area);
          session.getTransaction().commit();
      } catch (HibernateException he) {
          session.getTransaction().rollback();
  
          throw new ExceptionHandler(he.getCause());
      } finally {
          if (session != null) {
              session.close();
          }
      }
  
  }
  </code></pre>
  
  <p>But I want to use the DAO's generated by Hibernate Tools because they have EntityManager (which is supposed to be injected, but isn't apparentely).</p>
  
  <p>What I have to do ? Any idea ?
  Sorry by the long question, but I wanna to be very clear about my problem.</p>