Hello,
I'm successfully using mappings which reference objects in different data stores / hibernate sessions. Per suggestions in the forum, I accomplished this using a UserType which knows which hibernate session to load a class from.
A problem occurs when using the UserType to load objects from multiple sessions in conjunction with caching. I'm hoping for feedback on:
Code:
[b]Steps:[/b]
1) Object #1 is loaded in session A and contains a UserType property which loads object #2 from session B. Neither is yet in the JCS cache. Everything is fine.
2) Both sessions are then closed. Both objects are in the JCS cache.
3) Load object #1 again which now comes out of the JCS cache and of course is reassociated with a new session. BUT, object #1 now contains a reference to a transient instance of object #2! Object #2, which was previously loaded via a UserType, has no way of getting "re"associated with a new session and is forever transient *from the perspective of object #1*.
Hibernate obviously handles this reloading automatically with a "real" many-to-one where the objects are in the same session. But, since I'm basically "recreating" a many-to-one using different hibernate sessions and a UserType, I need to manage this problem manually.
Code:
[b]Possible solutions to ensure that object #2 is not transient, and gets reassociated with a new session:[/b]
1) Don't use a UserType at all, and instead call "load" every time the getter for object #2 is called from object #1. This is kind of messy and not really "natural". A UserType would be much more elegant.
2) Use the lifecycle callback "onLoad" on object #1. Loop through all of object #1's getters using reflection and "reload" any objects that are from a different session. So when object #1 is loaded, we'd loop through the getters and find object #2 which would then be "re"loaded from the new session, and no longer transient. This seems doable, but also inelegant. The power of the UserType for loading object #2 was used when object #1 came out of the database, but NOT when object #1 came out of the cache.
3) Don't cache anything. Not really a solution! :)
4) Any others?
A possible addition to UserType to help with this problem: Have the option to have a UserType's nullSafeGet method called every single time an object is loaded whether from cache or database. Right now, it just calls it when loading it from the database. This would fix the problem because the UserType could "refresh" itself every time the object with the UserType gets loaded (whether from database or cache). I guess this is basically what I'm recreating in scenario #2 above only it would be more elegant to handle this in the UserType itself.
In short, hibernate only seems to call the UserType when loading from the database. It would be nice if there was an option to have it call the UserType when loading an object from the cache as well.
Any and all suggestions are welcome. :) Please let me know if I can elaborate! It seems like objects with references to objects in other sessions should be elegantly handled via UserTypes. But, scenario #2 doesn't seem all that elegant.
Thanks,
Ryan