JPA: How to get Id after persist in standalone java app

Go To StackoverFlow.com

4

This is a standalone java application, not web application. So when I persist the object like this

public <T> T create(T t) {
    em.getTransaction().begin();
    em.persist(t);
    em.flush();
    em.getTransaction().commit();
    return t;
}

The id inside the T t 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 @EJB, the id 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?

This is how I mapped my id inside my @Entity Class

@Id
@Basic(optional = false)
@Column(name = "ID")
private Long id;

also I make the id of this table in the database AUTO_INCREMENT, like this

CREATE TABLE Config
(
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (ID)
)

This is how I obtain my EntityManager

EntityManagerFactory emf = Persistence.createEntityManagerFactory("CorePU");
em = emf.createEntityManager();

Here is what inside my persistence.xml

<persistence-unit name="CorePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>    
<class>com.wf.docsys.core.model.Config</class>    
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/XNINFODB"/>
  <property name="javax.persistence.jdbc.password" value="xxx"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="xxx"/>
</properties>

Please help, I am not sure what I did wrong here.

2012-04-05 21:03
by Thang Pham


0

Here is my answer. Tested

When mapping your Id, switch from what I have above, which is

@Id
@Basic(optional = false)
@Column(name = "ID")
private Long id;

to

@Entity
public class Person {
   @Id
   @TableGenerator(name="TABLE_GEN", table="SEQUENCE_TABLE", pkColumnName="SEQ_NAME",
       valueColumnName="SEQ_COUNT", pkColumnValue="PERSON_SEQ")
   @GeneratedValue(strategy=GenerationType.TABLE, generator="TABLE_GEN")
   private long id;
   ...
}

Since I use microsoft SQL server, I have to use @TableGenerator. More information can be found here http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Table_sequencing

2012-04-05 23:49
by Thang Pham


4

Try adding a @GeneratedValue on your id field.

2012-04-05 21:21
by barsju


1

As I found on this post the EntityManager have a method called refresh that will update your object after you persist him.

So your code will probably looks like this:

public <T> T create(T t) {
    em.getTransaction().begin();
    em.persist(t);
    em.refresh(t);
    em.flush();
    em.getTransaction().commit();
    return t;
}

I haven't tested this, so I'm not sure where exactly to put the refresh method call, there, after the commit, after the flush, etc.

Hope it can help you.

Good luck.

2012-04-06 00:04
by Guedes
refresh is unnecessary there. Just do flush(). That forces the persist, and hence obtaining the id (once the user has added GeneratedValue annotation of course) - DataNucleus 2012-04-06 10:59
@DataNucleus: Yup, I have posted my answer below, since I cannot use @SequenceGenerator, I use @TableGenerator with@GeneratedValueand it works. However, will callingflush()` every time cause performance problem, DataNucleus - Thang Pham 2012-04-06 13:05
@DataNucleus: flush() worked for me. You should post an answer. And if your well versed enough also address Thang Pham's performance concern - user2316667 2014-06-23 19:11


1

I know it's not a persist but the merge will do the same thing more simply. I wrap my persist in an actual merge operation which will save the entity if it does not exist.

public T persist(T obj) {
    EntityManager em = ThreadLocalPersistenceManager.getEntityManager();

    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        obj = em.merge(obj);
    } finally {
        if (! tx.getRollbackOnly()) {
            tx.commit();
        }
    }

    return obj;
}


/**
 * This will save the object and return the id of the persisted object.
 *  
 * @param obj
 * @return The id of the persisted object.
 */
public Long save(T obj) {
    persist(obj);
    return obj.getId();
}
2013-02-22 22:53
by Chris Hinshaw
Ads