How to do bulk delete in JPA when using Element Collections?

前端 未结 1 1651
日久生厌
日久生厌 2020-12-29 08:18

I am having trouble working out how to do a bulk delete of a Person object using JPA, when the Person objects contain data stored using an @ElementColl

相关标签:
1条回答
  • 2020-12-29 08:51

    I'll let you interpret the part of the JPA 2.0 specification that mentions that a bulk delete operation is not cascaded:

    4.10 Bulk Update and Delete Operations

    ...

    A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.

    And the fact is that Hibernate won't cascade a delete to a collection table either. This has been reported in HHH-5529 and the suggested approaches are:

    You could also (a) clean up the collection table yourself or (b) use cascading foreign keys in the schema.

    In other words, (a) use native SQL or (b) use a cascade delete constraint at the database level - and you'll have to add it manually, I don't think you can use @OnDelete with the @ElementCollection annotation (same story as HHH-4301 IMO).

    0 讨论(0)
提交回复
热议问题