OpenJPA – การใช้ merge

การ Update Entity ที่ไม่อยู่ใน Persistance Context ด้วย คำสั่ง merge

// Update Entity ที่ไม่อยู่ใน Persistance Context
Oltp oltp = em.find(Oltp.class, tranId);
System.out.println("is oltp in p-context="+em.contains(oltp)); // true
em.detach(oltp); // เอา entity oltp ออกจาก persistance context
System.out.println("is oltp in p-context="+em.contains(oltp)); // false

//Modify
em.getTransaction().begin();
oltp.setLastUpdateDate(new Date());
em.merge(oltp); // ใช้ update entity ที่ไม่อยู่ใน persistance context
System.out.println("is oltp in p-context="+em.contains(oltp)); // false
em.getTransaction().commit();

การ Update Entity ที่อยู่ใน Persistance Context

// Update Entity ที่อยู่ใน Persistance Context 
Oltp oltp = em.find(Oltp.class, tranId);
System.out.println("is oltp in p-context="+em.contains(oltp)); // true

//Modify
em.getTransaction().begin();
oltp.setLastUpdateDate(new Date());
System.out.println("is oltp in p-context="+em.contains(oltp)); // true
em.getTransaction().commit();

About WK

Kajook is a Senior Software Engineer. Job is Design & Develop in Java EE environment on Glassfish. I'm Interested in Agile software development ,iOS and Android.
This entry was posted in knowledge and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s