Hi folks -
Can anyone give me some general advice on an issue I am having with the refresh function? Let me apologise in advance for not filling in the bullet points provided but my question is more of a general nature so it didn't seem helpful to post the versions etc which i am using.
To explain the issue, here's a simple example:
I have 2 classes: ClassA & Class B. ClassA has a list of ClassB's, which are defined as a one to many relationship (1 A to many B's). The list is mapped via a bag with cascade set to all. Both classes have their own tables in the database & ClassB's table has an additional column to hold the identifier of the class A which owns it.
Now, when i run my application and load an instance of ClassA (let's call it ObjectA), it loads all the instances of ClassB, which are associated with ObjectA, into the session as well. Similarly, if i call the refresh function on ObjectA, it cascades to all the associated ObjectB's and they refresh as well.
My issue is when something outside the hibernate session removes one of the objectB's (lets call the removed item ObjectB1) from the database. An example could be another user with another version of the application also working on a common database. When the first user then tries to refresh ObjectA, it still holds a memory reference to ObjectB1. It then tries to refresh ObjectB1 through the cascade, and throws a KeyNotFound exception because it is no longer in the database. I would have expected this to have returned an up to date version of ObjectA, with its list of ObjectB's minus the removed item, but obviously it doesn't..
So my question is this: How do you handle it? My first thought was to refresh each ObjectB seperately. This way i could catch the exception and easily remove the offending item from the session / application but this is essentially doing the cascade manually which defeats the point of using it. Secondly, the real life example is not as simple as this. In reality ObjectB has many ObjectC's which in turn have many ObjectD's and so on. Trying to do this manually would mean a hell of a lot of extra code.
Thanks in advance to anyone that can help, I've been hitting my head against this hibernate-shaped brick wall for a couple of days now lol
|