OK first and foremost, please please please break up that method into more than one method. it is so long that I can't read it. By breaking it up into methods that do one thing and are smaller will make readability and maintainability much easier.
So I am going to help you by just making assumptions on what the code is.
I think you have a detached object, which is sent to the client, it comes back detached and you need to reattach the object. You will need to use a method like merge() because there is another instance of your object within the Session's Persistent state with that object's id.
Here is an example.
Lets say I have a list with an Object in it
Lets just say that a List won't allow you add an object if there is another object in the list with the same id, and we assume that a and b have the same id. The last line of code will fail. This is basically what is happening. Now let's say that a List had a method called merge, that will make is such that you can add an object with the same id, but only though that method
so then myList.merge(b); will not cause an error and would replace object a, which is exactly what you want in your code.
Mark