hibernate - Hibernate3 many to many cascade deleting with cleanup -


i have problem cascade deleting in hibernate 3.6. have following many many relationship between 2 entity classes: menu , item. note uni-directional relationship. menu instance has reference set of items has. item instances not know menu instances belongs to.

@entity public class menu {      @id     private long id;      @manytomany(cascade = {cascadetype.all})     @jointable(         name = "menu_item",         joincolumns = {@joincolumn(name = "menu_id")}         inversejoincolumns = {@joincolumn(name = "item_id")},     )     private set<item> items = new hashset<item>();      .... }   @entity public class item {      @id       private long id;       private string name;      private string code;      ... } 

my requirement: row in item table should exist in database if @ least 1 menu entry refers it. is, item not belong menu should not exist in database.

now, assume following scenario,

menu1 has [item1, item2]

menu2 has [item1]

when invoke session.remove(menu1), hibernate tries remove both item1 , item2 item table (this operation rejected dataset). want hibernate remove item2, since there menu using item1.

i'm aware can prevent associated items being deleted when menu deleted making cascade not include cascadetype.delete. don't want behavior either: want item2 deleted database (since no menu using it, i.e., has no associations in menu_item table).

what best achieve in hibernate?


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -