开发者

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 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜