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. – Entity class

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")
public class Student implements Serializable {

private static final long serialVersionUID = 1L;
@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) { = id;

public String getName() {
return name;

@Column(name = "name")
public void setName(String 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();



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

Now let us see the main class.

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
EntityManager entityManager = entityManagerFactory
if (null != entityManager) {
EntityTransaction readTransaction = entityManager.getTransaction();
Query query = entityManager
.createQuery("select student FROM Student student");
List list = query.getResultList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Student student = (Student);
System.out.println("Student Details : " + student);

public void updateRecord() {
EntityManagerFactory entityManagerFactory = Persistence
EntityManager entityManager = entityManagerFactory
if (null != entityManager) {
EntityTransaction updateTransaction = entityManager
Query query = entityManager
.createQuery("UPDATE Student student SET student.level = 'L' "
+ "WHERE :id");
query.setParameter("id", 1);
int updateCount = query.executeUpdate();
if (updateCount > 0) {

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



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.


Before update..

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


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 Delete Query Example


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

2 thoughts on “JPA Update Query Example

  1. Rahul Munjal Reply

    I tried your given example, my database is not getting updated. I am getting updateCount 1

Leave a Reply

Your email address will not be published. Required fields are marked *