Hello,
I am looking for solution for this kind of problem:
Let's assume we have an entity 'Item' and it is associated many-to-many with item 'Category'. Something like this:
Code:
public class Item
{
...
Set<Category> categories;
...
}
Code:
public class Category
{
...
Set<Item> items;
}
Now, if I delete one Category entity, every associated Item that has been already loaded, must be notified about this deletion (by removing deleted Category entity from it's
categories collection). Doing this assures consistency of in-memory model.
One way to do this is to iterate through all Items associated to the Category being deleted, and remove this entity from
categories collection, like this:
Code:
public void deleteCategory(Category category)
{
for(Item items : item)
{
item.remove(catgeory);
}
}
But this leads to initializing whole
items collection, possibly causing unnecessary loading of many, many Items objects. For example, if Category is associated to 10 000 Items and my business logic does not need and does not use any of them in a particular Hibernate session, that means loading of 10 000 objects in vain.
I think this 'notification' should only be send to Items that has been already loaded in session (prior to deletion of Category). But I cannot find any solution to get collection of objects loaded to the session. Maybe this is wrong way of thinking? I would like to hear your opinion about this topic. Maybe there are some patterns I haven't found?