Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework/JPA: Find using dot notation works but same query using delete does not

Let's say I have a couple of entities that look like this:

@Entity
public class Person extends Model {
    @ManyToOne
    @JoinColumn(name = "fooId", nullable = false)
    public Foo foo;
    ...
}

@Entity
public class Foo extends Model {
    @ManyToOne
    @JoinColumn(name = "barId", nullable = false)
    public Bar bar;
    ...
}

Now let's say I want to find all People that are not associated with a particular instance of Bar:

Person.find("foo.bar <> ?", someInstanceOfBar).fetch();

This works fine and returns all the People I would expect.

However, if I tweak the statement above slightly to delete all People that are not associated with Bar:

Person.delete("foo.bar <> ?", someInstanceOfBar);

I get the following:

PersistenceException occured : org.hibernate.exception.SQLGrammarException: could not execute update query
...
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "DELETE FROM PERSON CROSS[*] JOIN FOO FOO1_ WHERE BARID<>? ";

Any ideas why finding People would return the correct number of rows, yet deleting People using the same where-clause would result in an exception?

like image 524
digiarnie Avatar asked Nov 29 '25 00:11

digiarnie


1 Answers

The problem is that you have a Join in the delete statement. Delete in JPQL only accepts "where" clauses, and a unique table as target. Your join breaks that condition. See Oracle documentation on JPQL

like image 138
Pere Villega Avatar answered Nov 30 '25 15:11

Pere Villega



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!