[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5734) Restrict update or delete using Criteria

Joachim Durchholz (JIRA) noreply at atlassian.com
Tue Nov 16 06:14:13 EST 2010


Restrict update or delete using Criteria
----------------------------------------

                 Key: HHH-5734
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5734
             Project: Hibernate Core
          Issue Type: New Feature
          Components: core
            Reporter: Joachim Durchholz


I'd like to do something like

session
.createCriteria (MyEntity.class)
.add (Restrictions.eq ("someField", someValue))
.createQuery ("update set updatedField = :newValue")

(afterwards adding parameters and executing the thing, of course).

Currently, I need to do something like

session.createQuery ("update MyEntity set updatedField = :newValue where someField = :someValue")

To make the use case convincing, let me say that:
* some Restrictions are determined by the GUI, others by the application logic;
* the Restriction sets are merged at some point;
* before firing off the query, I need to check whether there's any Restriction at all to see whether I need a WHERE in the first place, then I need to wrap each Restriction's toSqlString result in parentheses and intersperse them with ANDs.
* This also means I'm forced to use SQL instead of HQL, meaning I need pass around SQL metadata together with a Restriction object to be able to actually generate SQL from it.
* One of the points of using Hibernate is not having to generate SQL (or HQL) on the fly, since that's somewhat error-prone. This works beautifully for SELECT, but not for UPDATE (or DELETE: having Criteria support for DELETE would be sweet, too).

The createQuery call above is probably not the way to go, it could be better to have something like createUpdate ("set updatedField = :newValue"), or maybe even something like addUpdate ("updatedField", ":newValue").
The result of such a call could be an Update object that embodies an UPDATE call, similarly to Criteria which embodies a SELECT.
Again, this could be extended to having createDelete, and a Delete type that embodies DELETE statements.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list