title: JPA: How to get Id after persist in standalone java app
body: <p>This is a standalone java application, not web application. So when I persist the object like this</p>
  
  <pre><code>public &lt;T&gt; T create(T t) {
      em.getTransaction().begin();
      em.persist(t);
      em.flush();
      em.getTransaction().commit();
      return t;
  }
  </code></pre>
  
  <p>The <code>id</code> inside the <code>T t</code> object is still null, even though a new row with correct data and id is created correctly inside the database. Usually in my web app that utilize <code>@EJB</code>, the <code>id</code> available right after I persist, since it persist the entity object into my persistence context, I am not sure if I have my persistence context here?</p>
  
  <p>This is how I mapped my id inside my @Entity Class</p>
  
  <pre><code>@Id
  @Basic(optional = false)
  @Column(name = "ID")
  private Long id;
  </code></pre>
  
  <p>also I make the id of this table in the database <code>AUTO_INCREMENT</code>, like this</p>
  
  <pre><code>CREATE TABLE Config
  (
      ID int NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (ID)
  )
  </code></pre>
  
  <p>This is how I obtain my EntityManager</p>
  
  <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("CorePU");
  em = emf.createEntityManager();
  </code></pre>
  
  <p>Here is what inside my <code>persistence.xml</code></p>
  
  <pre><code>&lt;persistence-unit name="CorePU" transaction-type="RESOURCE_LOCAL"&gt;
  &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;    
  &lt;class&gt;com.wf.docsys.core.model.Config&lt;/class&gt;    
  &lt;properties&gt;
    &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/XNINFODB"/&gt;
    &lt;property name="javax.persistence.jdbc.password" value="xxx"/&gt;
    &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
    &lt;property name="javax.persistence.jdbc.user" value="xxx"/&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p></p>
  
  <p>Please help, I am not sure what I did wrong here. </p>