Home Jpa Jpacrud JPA update data Example

Related Tutorials


 
 

Share on Google+Share on Google+

JPA update data Example

Advertisement
In this section, you know how to update the database data through the jpa.

JPA update data Example

     

In this section, you know how to update the database data through the jpa. You follow the following steps for updating the data. To update the data into database you can follow the following steps:

  • Load the business object
  • Update the data
  • Save (persist) the data using JPA merge() method

Follow the following steps to develop the example code to update the data into database.

Create a "JPAUpdate.java" file.

Put the following text.

Update code:
/**

*/
package roseindia;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
* @author Administrator
*
*/
public class JPAUpdate {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em = emf.createEntityManager();
try{
em.getTransaction().begin();
//find 
Student stud1 = em.find(Student.class,1);
stud1.setSname("Deepak");
//update
em.merge(stud1);
em.getTransaction().commit();
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
em.close();
}
}

}
Run it and get the output in "Console window" of eclipse.

Check it in MySQL.
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 24, 2009

Related Tutorials

Discuss: JPA update data Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
LATIN
April 15, 2011
UPDATE BLOB WITH JPA

Hi, How i can to update a column blob with jpa? Thanks
Daniel Guzman
November 15, 2011
gratitude

thank you very much!!! I have been looking for something like that for 3 hours... =D
Shiru
June 18, 2012
Thx for this tutorial

After 3:30h of search this was the first helpful tutorial to fix my updateproblem...
DMCA.com