title: How do I create an instance of my EntityManagerFactory by using my persistence.xml?
body: <p>I am creating a backend application using Spring, Hibernate and JPA.
  Currently the application test pass but I get a warning:
  WARN: HHH000436: Entity manager factory name (JpaPersistenceUnit) is already registered.</p>
  
  <p>I think the reason for this is that I defined my JpaPersistenceUnit in the persistence.xml and I also create one in my dao class. If this is the case I need to find a way to get my JpaPersistenceUnit from the persistence.xml without creating it (again). But I don't know how...</p>
  
  <p>This is my persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"&gt;
  &lt;persistence-unit name="JpaPersistenceUnit"
                    transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.archive.autodetection" value="class, hbm"/&gt;
          &lt;property name="hibernate.show_sql" value="true"/&gt;
          &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="hibernate.connection.password" value="groepD"/&gt;
          &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost/groepd"/&gt;
          &lt;property name="hibernate.connection.username" value="groepD"/&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
      &lt;/properties&gt;
  
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>This is my generic dao class:</p>
  
  <pre><code>public interface GenericDao&lt;E, ID extends Serializable&gt; {
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("JpaPersistenceUnit");
  public void add(E entity);
  public void remove(E entity);
  public void update(E entity);
  public E findById(ID id);
  public List&lt;E&gt; findAll();
  }
  </code></pre>
  
  <p>This is the specific dao class:</p>
  
  <pre><code>public interface TripDao  extends GenericDao&lt;Trip,Integer&gt; {
  }
  </code></pre>
  
  <p>And this is an implementation of the dao class:</p>
  
  <pre><code>@Repository
  public class TripDaoImpl implements TripDao {
  
  protected EntityManager entityManager;
  
  public TripDaoImpl() {
      entityManager = emf.createEntityManager();
  }
  
  @Override
  @Transactional
  public void add(Trip entity) {
      entityManager.getTransaction().begin();
      entityManager.persist(entity);
      entityManager.getTransaction().commit();
  }
  
  ....
  }
  </code></pre>
  
  <p>This is the entity:</p>
  
  <pre><code>@Entity
  @Table(name = "T_TRIP")
  public class Trip {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;
  
  @NotNull
  private String name;
  
  @ManyToMany(cascade = CascadeType.ALL)
  @JoinTable(name="T_TRIP_ADMINS",
          joinColumns={@JoinColumn(name="tripId")},
          inverseJoinColumns={@JoinColumn(name="userId")})
  private Set&lt;User&gt; admins;
  
  @ManyToMany(cascade = CascadeType.ALL)
  @JoinTable(name="T_TRIP_PARTICIPANT",
          joinColumns={@JoinColumn(name="tripId")},
          inverseJoinColumns={@JoinColumn(name="userId")})
  private Set&lt;User&gt; invitedUsers;
  
  @NotNull
  private Boolean privateTrip;
  
  @NotNull
  private Boolean published;
  
  @Enumerated(EnumType.STRING)
  private TripType type;
  
  @NotNull
  private Integer nrDays;
  
  @NotNull
  private Integer nrHours;
  
  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name = "tripId")
  private Set&lt;Stop&gt; stops;
  
  public Trip(){
      initLists();
  }
  
  private void initLists(){
      this.admins = new HashSet&lt;User&gt;();
      this.invitedUsers = new HashSet&lt;User&gt;();
      this.stops = new HashSet&lt;Stop&gt;();
  }
  
  public void addStop(Stop stop) {
      stops.add(stop);
  }
  
  public boolean removeStop(Stop stop) {
      if (stops.size() &gt; 1 &amp;&amp; stops.contains(stop)) {
          stops.remove(stop);
          return true;
      } else {
          return false;
      }
  }
  ...More getters and setters...
  }
  </code></pre>
  
  <p>If someboby could tell me how to to fix the warning that would be very helpfull.</p>