Is there a way to update a database field based on a list?

Using JPA, I have a list of records from my database:

User(id, firstname, lastname, email)

What am I doing:

List<User> users = User.find("lastname = ?", "smith");

And I would like to update everything in one request by doing something like this:

"UPDATE USER SET email = null IN :list"

then set the list parameter to users

Is it possible? if so, how?

Thank you for your help:)

+3
source share
2 answers

Well, you can embed the query that you used to get the list in the where clause for the update.

UPDATE User a SET a.email = null 
WHERE user IN (SELECT b FROM User b WHERE lastName = :?)

By doing this, you will run a query to search the list and update in a single update request.

How do you like it? Do you think this might work?

-Edit -

, , , ,

UPDATE User a SET a.email = null 
WHERE user IN (SELECT b FROM User b WHERE lastName IN(:originalList))

, , - :

Collection<String> originalList = Arrays.asList("Kenobi", "Skywalker", "Windu");
query.setParameter("originalList", originalList);

, , , , , .

+3

jpa + hibernate, entityManager.createQuery() hql :

String hql = "UPDATE Supplier SET name = :newName WHERE name IN :name";
entityManager.createQuery(hql);
0

All Articles