JPA Update Query Example

So far we have discussed the basics of Java persistence API. We discussed the history , the various concepts etc. In the just previous chapter we  discussed about the insert and fetch operations with relational database using JPA.We used the slect query in JPA Query Language(JPQL) to fetch the contents from database.  In this chapter we are looking into the JPQL Update query.

Update can be either a)Selective Update , or b) Update All

In this chapter we are discussing a selective update example. Selective update needs WHERE  clause to select a particular row from the table.

JPA Update Query Example

We are using the same set of data we inserted to the database earlier. The project and its setup are same as explained in the previous chapter.There we inserted one Student object into the database table. In this chapter we are just updating the existing record. The entity class  and the persistence.xml are same . The database configuration and all other required things were explained in the previous  chapter.

Student.java – Entity class

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 let us see the main class.

UpdateSample.java

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.jpa.entity.Student;

public class UpdateSample {

public UpdateSample() {

}

public void readDetails() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("OpenJPASample");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
if (null != entityManager) {
EntityTransaction readTransaction = entityManager.getTransaction();
readTransaction.begin();
Query query = entityManager
.createQuery("select student FROM Student student");
List list = query.getResultList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student) iterator.next();
System.out.println("Student Details : " + student);
}
readTransaction.commit();
}
}

public void updateRecord() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("OpenJPASample");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
if (null != entityManager) {
EntityTransaction updateTransaction = entityManager
.getTransaction();
updateTransaction.begin();
Query query = entityManager
.createQuery("UPDATE Student student SET student.level = 'L' "
+ "WHERE student.id= :id");
query.setParameter("id", 1);
int updateCount = query.executeUpdate();
if (updateCount > 0) {
System.out.println("Done...");
}
updateTransaction.commit();
}
}

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

}

}

Update Query is not retrieving any data from database . It simply updates the database directly.It is a good practice to use a separate EntityManager  for update query.

Output

Before update..

Student Details : ID = 1 ; Name : = Noby ; Level : = H

Done…

After Update

Student Details : ID = 1 ; Name : = Noby ; Level : = L

JPA Update All

In the above we used a conditional update query to update a particular row. If we execute the query:

UPDATE Student student SET student.level = ‘L’ , the entire  table will be affected.The entire rows will be having level as ‘L’ then.

See Related Discussions

JPA Overview

JPA Read/Insert Example

The find() method in JPA

Updating a record using JPA

JPA Delete Example

JPQL

JPQL Delete Query Example

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