Farid
Farid

Reputation: 1572

hibernate simple query with join not working

I'm trying to delete some BlRelation in my database using hibernate.

Unfortunately the query does not work. Could you please help me on this ? I know it should be quite straightforward ...

delete r FROM BlRelation where r.elementId.blProvider.providerId = 1

Thanks !

Upvotes: 0

Views: 272

Answers (2)

JB Nizet
JB Nizet

Reputation: 691635

Quote from the reference manual:

The pseudo-syntax for UPDATE and DELETE statements is: ( UPDATE | DELETE ) FROM? EntityName (WHERE where_conditions)?.

Some points to note:

  • [...]
  • No joins, either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.

You could just use a subquery, or simply get the entities in the session and use session.delete() to delete them.

Here's a subquery that would probably work:

delete from BlRelation r where r.id in 
  (select r2.id from BlRelation r2 where r2.elementId.blProvider.providerId = 1)

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

Please provide more details about your mapping, without it I am only guessing. But try this:

delete FROM BlRelation where elementId.blProvider.providerId = 1

Also is elementId a field of BlProvider type? I guess it is named element, so it should have been:

delete FROM BlRelation where element.blProvider.providerId = 1

Finally if element is in one-to-many or many-to-many relationship with BlProvider (it is of collection type), you will need some more complicated query.

Upvotes: 1

Related Questions