Why doesn't JPA CriteriaQuery provide an update request?

CriteriaQuery in JPA2.0 provides a safe way to choose, which is great. But I wonder why it will not perform the update / delete operation? For bulk updates / deletes you will have to revert to the old time, exposing SQL or JPQL text errors. IMO, CriteriaQuery for updating / deleting should not be difficult, since the processing of the reason is the same when choosing.

Hope this will be implemented in the next version of JPA.

+5
source share
1 answer

Since JPA is an ORM tool that encourages mapping database records to objects in such a way that database records can be manipulated using a programming language instead of SQL, this does not encourage the use of SQL / HQL / JPQL to perform an upgrade.

To prevent the use of error-prone SQL or JPQL for updating objects, as soon as you use CriteriaQueryto retrieve a list of objects, you can simply skip the result objects and then change the properties of the object one at a time to do a bulk update or delete. JPA should be able to detect changes made to these objects and generate appropriate SQL queries to update the corresponding records in the package when called EntityManager.flush().

+1
source

All Articles