JPA Update Example

We already discussed the fundamentals of JPA. We discussed the steps in configuring a JPA project in eclipse.In this chapter we are looking into how an existing record can be updated using  JPA.Alternately we can  update an existing record by using JPQL Update Query also.

JPA Update Example

Here also OpenJPA is using as JPA Implementation.In this example we are trying to update an existing Student object.We are using the same set up explained in the earlier chapter.The same entity class and persistence.xml are using here also.

Student.java

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity(name = "Student")
@Table(name = "student")
public class Student implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String level;

public Student() {

}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

@Column(name = "name")
public void setName(String name) {
this.name = name;
}

public String getLevel() {
return level;
}

@Column(name = "level")
public void setLevel(String level) {
this.level = level;
}

public String toString() {
return "ID = " + getId() + " ; Name : = " + getName() + " ; Level : = "
+ getLevel();
}

}

persistence.xml

org.apache.openjpa.persistence.PersistenceProviderImpl com.jpa.entity.Student

Now  lets see the UpdateSample.java which contains the main .The steps to update an existing record are:

  • Fetch the required object from Database
  • Make changes in the object parameters(except primary key attribute)
  • Persist the object to the database

In this example we are updating the level attribute of a Student object .

UpdateSample.java

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

import com.jpa.entity.Student;

public class UpdateSample {

public void doRead() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("OpenJPASample");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
if (null != entityManager) {
EntityTransaction readTransaction = entityManager.getTransaction();
readTransaction.begin();
Student student = entityManager.find(Student.class,251);
System.out.println("Student Details : "+student);
readTransaction.commit();
}
}

public void update() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("OpenJPASample");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
if (null != entityManager) {
EntityTransaction updateTransaction = entityManager
.getTransaction();
updateTransaction.begin();
Student student = entityManager.find(Student.class,251);
student.setLevel("L");
entityManager.persist(student);
updateTransaction.commit();
}
}

public static void main(String[] args) {
UpdateSample sample = new UpdateSample();
System.out.println("Before Update..");
sample.doRead();
sample.update();
System.out.println("After update...");
sample.doRead();

}
}

Output

Run the UpdateSample.java.This application updates an object with primary key value as 251.Before update the level attribute value was ‘L’. The application updates the level attribute value as ‘H’.

Before Update..

Student Details : ID = 251 ; Name : = Bijoy ; Level : = L

After update…

Student Details : ID = 251 ; Name : = Bijoy ; Level : = H

See Related Discussions

JPA Overview

JPA Example

find method in JPA example

JPA Delete Example

JPQL

JPQL Update Query Example

JPQL Delete Query

Caching

Overview to JPA Caching

Locking in JPA

Locking in JPA

Relational Mappings in JPA

One To One Mapping in JPA

One To Many mapping in JPA

Many To Many Mapping in JPA

Many To One Mapping in JPA