DWright wrote:
For our rich client app, we are actually passing the entire entity to the client. Since our client & server communicate using web services (Axis), the Hibernate-specific collection types get converted to plain java.util... types on the client, and stay that way when sent back to the server. In our DAO, we then don't need a session.get. We just do a session.merge(obj). The problem with the version number that you found appears that it is getting the version number from the version that is in the session cache. Two unpleasant ways I know to resolve this - use session.evict(obj) after you do the session.get (but this would have to be done for each object loaded by Hibernate - I don't know if an interceptor would help or if Hibernate would hate the fact that a loaded object is not found in the cache. Probably the later, especially in the case of bidirectional relationships or other circular relationships). Of course, if you're using DTOs, you are probably explicitly going a get for each object to update anyways. Another way is with session.clear after you do all of your session.gets.
Thanks for your reply. i have gone with the Transfer Object pattern because of the performance we gain from them and also i love hibernate in it's pure sense which i think from the web perspective is the opensessionview or can be clearly seen in a standalone app. What i mean is the lazy loading is seamless and you don't have to deal with all this helper code to manage objects in and out of sessions. This helper code seems to be quite messy and one of the reasons i chose dtos was to avoid having to get my hands dirty with it. I like DTOs because there is no question about what you get. When you get an evicted hibernate object you have to always be on your guard as some methods that would normally load say collections or do some logic with child relationships won't work. With these truly detached remote applciations this just doesn't seem right to me to give the view an object that has publicly available methods that just won't work. Of course this is my first real hibernate project so i am still very inexperienced and naive to all it's options.
Anyways your suggestions are good ones, they do bring some messiness into the code but they also are practical and possible so it might be worth it. So from your suggestions, i do get the sense that you believe the technique of storing the version in the DTO and reassembling the domain with it when i do an update isn't a horrible idea?
Cause while i am very unhappy with this, i do consider it to be a bug in hibernate, as i continue to analyze the situation i wonder if there is a better overall approach to handle this concurrency issue when using the dto/transfer object pattern. I have asked this question in every possible outlet i can think of but i haven't gotten much back in terms of best practices, probably cause most people, generally coming from a webapp perspective think the Transfer Object pattern is evil. ANwyays anymore feedback would be greatly appreciated.