I currently get the impression, that I have not yet understood one of the key-concepts of the EntityManager. Maybe someone can help me out a little bit.
I have the current scenario, that I´m running a client-server-seam-application using hibernate 3 together with Spring 2.5. The persistence-context is NOT set to extended, but the Seam-managed PC is used.
As usually in client-server-environments, the client asks for an object using a service. Let´s say he asks for a user. An user-entity will be returned using Hibernate. After the user-entity has left the server it will be detached from the hibernate-session, which is okay for me.
Now let´s say I changed something on the user-entityand would like to send the update to the server. When this user-entity is returned to the server again, it is still unmanaged. So far so good.
Now I´m always asking myself how to reattach this entity to the session again. In many of my services I perform some actions on the entity, before I think it is okay to merge the changes to the database. Let´s say the user has a collection of usergroups and I would like to iterate through this collection before. As this entity is unmangaged I get a LazyInitializationException which is totall understandable in this situation, but what can I do in this situation to prevent this Exception?
So I wonder which is the correct way to go now? Do I have to merge the entity as a first step, everytime it is returned to the service? But what, if I realize, that the entity is not as I expect it at a later step in my service? Do I have to roll back the transaction through throwing an Exception to prevent the merge?
I can´t imagine that I would have to fetch the object again in the service and would have to transferr all changes to this object myself, would I?
Furthermore everytime I merge the session, a new object will be returned. So there´s always a difference between the object I received from the client and the object I´m working with and possibly the client´s object does not have the correct data. So to be sure I would always have to refetch the entity after calling the service. But this sounds a bit strange to me and I wonder if this is the way to go.
As I´m probably not the first one using hibernate in a client-server-environment there have to be a lot of solutions around. So maybe someone can tell me, how to proceed in those cases as I´m a little bit los right now.
So I feel that there has to be a very basic concept in hibernate and maybe someone can point me the right directions.
Thank you very much