zakmck
zakmck

Reputation: 3004

HQL, can I parameterize the FROM clause?

I have this HQL query:

        Query q = em.createQuery (
        "DELETE FROM Annotation a WHERE a.id IN ( " +
        "  SELECT ja.id FROM :entityName an JOIN an.annotations ja)"
    );

and I'm being told: QuerySyntaxException: unexpected token: : near line 1 Do I have any hope of making the entity name after FROM a parameter? I have a list of entities to send to this query and I'm afraid that string concatenation is too slow.

Upvotes: 0

Views: 140

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153780

You can't substitute the Entity name the parameters work for entity properties not instead.

You could select the entities ids to be deleted with one query and then pass them to a second delete query, but for READ_COMMITED transaction isolation you might still end up with someone else inserting one child entity that would have matched your query. SERIALIZABLE will solve this issue.

Upvotes: 1

Related Questions