Announcement Announcement Module
No announcement yet.
Hibernate - can you control cascade DELETE programmatically? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate - can you control cascade DELETE programmatically?

    I have the following scenario in Hibernate:

    TAG (one-to-many) TAGGED_ARTICLE (many-to-one) ARTICLE

    When I delete a TaggedArticle (Im using HQL) I want to cascade the Tag and Result as well but only if they are not referenced by any other TaggedArticle.

    The only way I can do this so far I think is by:

    Manually check if there are more references to the Tag and Article that I want to potentially delete. If there are no other TaggedArticle's referencing the Tag and Result then I can either explicitly delete them each in turn, or, if I have cascade=DELETE declared in the mapping then I can just make sure the relevant and are set on the TaggedArticle and then do a:

    delete from TaggedArticle ta where = :ta_id
    Does anyone know if you can control CASCADE programmatically in DAO code?

    ie. if I do not declare cascade=DELETE in the mapping can I turn it on and off some way in the my code.

    ie. so I can do this which I think would be more elegant:

    - if no other Tag references exist then set cascade to true for this query for Tag
    - likewise for Article

    Thanks for any help

  • #2
    why are you doing deletes through HQL? if you do that you have to control all cascade "stuff" yourself. you'll be better served if you just remove tag from tag articles collection and then just save tag. you can also check whether your tag now has no references to tag articles and delete it. I would not try and do all this with HQL, not sure why you would even use hibernate if you are doing your operations through HQL?