开发者

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

开发者 https://www.devze.com 2023-02-24 17:00 出处:网络
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 w开发者_Python百科ant to write a JPQL that retrie

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 w开发者_Python百科ant 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


Here is the solution

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号