1 Reply Latest reply on Apr 9, 2006 10:24 AM by mazz

    many to many delete

    mazz

      I've seen a few things on the forums/JIRA on this, but I can't quite find what the actual solution is.

      I have a many-to-many relationship. The tables are typical, normalized ER-style:

      ENTITY_A
      idA
      nameA

      ENTITY_B
      idB
      nameB

      RELATION_AB
      idA
      idB

      When I go to delete an ENTITY_A, I can't CascadeType.REMOVE (spec seems to imply this is not required to be supported, if not outright disallowed) and I've seen some Hibernate JIRA issues talking about how this isn't supported) - so how do I delete the rows in RELATION_AB.

      I simply want to remove all rows from RELATION_AB where idA is the id of the deleted ENTITY_A.

      AFAICT, it looks like I have to create a native query and just execute SQL to do the delete.

      Is this true? If not, what does the EJBQL look like (not the native SQL)?

        • 1. Re: many to many delete
          mazz

          Well, I just found out you can't do a native query to perform deletes (learn something everyday :-). I get this after a createNativeQuery("DELETE FROM...") / executeUpdate():

          java.lang.UnsupportedOperationException: Update queries only supported through HQL

          I'm at a loss as to how I can delete the relationship rows from RELATION_AB while leaving ENTITY_B rows intact (e.g. I just want to remove the relationship, not the entities in that relationship).