views:

37

answers:

1

I have a Story entity with the following associations:

  • Story <1-*> Chapter
  • Story <1-*> Comment
  • Story <*-1> User

What is the correct way of removing this entity and handling the all the entities that is referring to? Is there some shorthand way of specifying that associated entities must be handled automatically or is the @PreRemove annotation mentionned in the article below a valid way of achieving this?

http://blog.xebia.com/2009/04/09/jpa-implementation-patterns-removing-entities/

+2  A: 

Depending on what you want to do - delete the remaining entities or retain then, you can use the cascade attribute of the @*To* annotations.

Bozho
Thanks. `Cascade` is already defined as `CascadeType.MERGE` on some entities. I'll ask my the other people on the project if this can be changed.
James P.
you can add `CascadeType.DELETE` to the `MERGE`. The `cascade` attribute accepts an array of cascade types
Bozho
Should be `CascadeType.REMOVE`, to be precise: http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#OneToMany
Péter Török
Yes, of course, thanks :)
Bozho