title: Hibernate EntityManager, is it supposed to be used as a singleton?
body: <p>I am not using Spring so I am creating an instance of EntityManager within a class.</p>
  
  <p>I used Hibernate-Eclipse reverse engineering to auto-generate the classes. These classes all has an instance of EntityManager.</p>
  
  <p>I am not 100% sure how Hibernate works with the EntityManager so I am wondering if it is okay that so many instances of this class (EntityManager) are made, for example, will there be problems with transactions?</p>
  
  <p>Should I just make a separate class that distributes a static instance of an EntityManager for all my other classes? or does it not matter?</p>
  
  <p>EDIT: I see there's something called @PersistenceContext, it doesn't seem to load my persistence.xml as a bean in to the instance variable, does this feature require spring? (I get null pointer exception, because it was never injected)</p>
  
  <p>snip of code from where I attempt to use @persistencecontext</p>
  
  <pre><code>@PersistenceContext(unitName = "manager1")
  private EntityManager entityManager;
  </code></pre>
  
  <p>my persistence.xml</p>
  
  <pre><code>    &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="manager1" transaction-type="RESOURCE_LOCAL"&gt;
           &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  
        &lt;properties&gt;
  
           &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
           &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
           &lt;property name="javax.persistence.jdbc.password" value="mypassword"/&gt;
           &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/ptbrowserdb"/&gt;
           &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;
        &lt;/properties&gt;
     &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>