Thang Pham
Thang Pham

Reputation: 38705

JPQL: how to retrieve a List of data and set a value for each data in that list

So I have a entity Notification, that has a boolean attribute readStatus to determine of a notification has been read or not. When I click a button, I want to write a JPQL that retrieve the unread notifications (readStaus=false) and set the readStatus of those unread notifications to true

Note: I dont want to write jpql to retrieve the list and manually loop through the list to set the readStatus and merge them back to the database

Upvotes: 0

Views: 493

Answers (2)

Thang Pham
Thang Pham

Reputation: 38705

Here is the solution

UPDATE Notification n SET n.readStatus = true WHERE n.readStatus = false

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691645

List<Notification> notifications = 
    em.createQuery("select n from Notification n where n.readStatus = false")
      .getResultList();
for (Notification n : notifications) {
    n.setReadStatus(true);
}

Upvotes: 0

Related Questions