title: Using generics and jpa EntityManager methods
body: <p>Can I use generics and JPA together? </p>
  
  <p>I am trying to persist objects of four classes to my db. Here's my PersistService class:</p>
  
  <pre><code>public class PersistService&lt;T&gt; {
  private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("fileUploadProject");
  
  public static EntityManager getEntityManager() {
      return emf.createEntityManager();
  }
  
  // Write Client to Database
  public static &lt;T&gt; void persist(T obj) {
      EntityManager em = getEntityManager();
      EntityTransaction et = em.getTransaction();
      et.begin();
      em.persist(obj);
      et.commit();
      em.close();
  }
  }
  </code></pre>
  
  <p>But then I get into a problem with removing the object. I have the following method in the PersistService class in addition to the above:</p>
  
  <pre><code>// Remove an object from the Database if they exist
  public static &lt;T&gt; void remove(Long id) {
      EntityManager em = getEntityManager();
      EntityTransaction et = em.getTransaction();
      &lt;T&gt; obj = em.find(&lt;T&gt;.class, id);
  }
  </code></pre>
  
  <p>The final line is giving me a compile time error. I've tried <code>&lt;T&gt;.class</code> <code>T</code> <code>Class&lt;T&gt;</code> and <code>T.class</code> as well, but it still gives me a compile time error. Just learning about Type Erasure, is this error because of that? How do I resolve this issue? </p>