Wrong SQL generated for delete with where crossing tables (derby, although hsqldb ALSO broken :()

Description

I'm trying to run this query:

delete StoredMessagePendingClient m where m.clientId = :clientid and m.message.messageId = :messageid

This generates the following SQL, which fails under derby because derby doesn't accept two tables seperated by a comma (I'm anyway not trying to delete from both tables, only from the first one, so that SQL is wrong).

Hibernate: delete from MESSAGES_PENDINGCLIENT, MESSAGES storedmess1_ where CLIENT_ID=? and ID=?

WARN: SQL Error: 20000, SQLState: 42X01
Apr 05, 2015 5:32:05 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Syntax error: Encountered "," at line 1, column 35.

Also note the same query also fails under hsqldb for different reasons (going to create another issue for it) so it's REALLY annoying

Attachments

1
  • 29 Oct 2015, 09:31 AM

Activity

Show:

Fred Toussi December 16, 2016 at 1:36 AM
Edited

This issue has also been reported for HSQLDB. As commented by Richard, Standard SQL does not support a joined table in a DELETE statement and a subquery is needed. Some databases add proprietary syntax to allow joined tables in DELETE but they should not be considered the norm.

Even in a SELECT statement, CROSS JOIN should never be used on a query that actually joins two tables on column equality. If both Dog and Breed tables have a column named breed_id, the most appropriate Standard SQL query is

SELECT FROM DOG NATURAL JOIN BREED BREED1_ WHERE NAME='collie'

Several database do not support NATURAL JOIN, in which case:

SELECT FROM DOG NATURAL JOIN BREED BREED1_ ON (DOG.BREED_ID = BREED1_.BREED_ID)
WHERE NAME='collie'

Richard Richard January 7, 2016 at 2:55 PM

We first run to this issue in autumn 2014 and while it can be worked over with subqueries we decided to stay on EclipseLink and I forgot it. Today I tested it again with 5.0.6.Final – I did it as a part of one heated discussion about how easy it is to swap JPA providers and both parties agreed there is nothing in JPA 2.1 specification that should stop this from working. I tested it with H2 and from JPQL:
delete from Dog d where d.breed.name = 'collie'
It rendered this SQL:
DELETE FROM DOG CROSS[*] JOIN BREED BREED1_ WHERE NAME='collie'
(not to mention that name field is both on Dog and Breed and is not qualified in the result)

Both entities are as plain as possible, relation looks like this:
@ManyToOne @JoinColumn(name = "breed_id") private Breed breed;

Test case can be run from shell like this:

$ svn export https://github.com/virgo47/litterbin/trunk/issues/hibernate-delete-where $ cd hibernate-delete-where/ $ mvn clean package

Fails during the test run.

JPA 2.1 specifiction in §4.10 describes bulk updates and refers to where_clause described in §4.5. For selects it works as expected.

Christian Beikov October 29, 2015 at 9:32 AM

I can confirm that hibernate 5 also tries to render a join which is obviously wrong for a delete query. See the attached test case. A possible workaround is to use a subquery that selects the ids and match them with an IN in the delete query.

Steve Ebersole October 28, 2015 at 3:25 AM

As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.

Steve Ebersole October 27, 2015 at 7:15 PM

This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.

For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Created April 5, 2015 at 4:42 PM
Updated December 20, 2016 at 10:03 PM