csharp_developer
csharp_developer

Reputation: 3

NHibernate - Lock Deserialized Object

I have a issue when I lock an object which is deserialized.

e.g.:

var session = sessionFactory.OpenSession();
var item = session.QueryOver<T>().Where(x => x.Id = "1").FutureValue().Value;

var serializedObject = Serializer.Serialize(item);

//do something with the serialized object
//...

var deserializedObject = Deserializer.Deserialze(serializedObject);
//lock record
session.Lock(deserializedObject, lockMode);

Error:

a different with the same identifier value was already associated with the session...

But the SessionId in the error is the same SessionId from the opened session.

If I lock the original "item" then it works.

Now is my question how can I interact with serialization/deserialization?

Thanks in advance and best regards

Upvotes: 0

Views: 74

Answers (1)

xanatos
xanatos

Reputation: 111920

NHibernate is tracking exactly one instance of your T... You are trying to use another instance of T (the one returned by Deserialize). You have to session.Merge the deserialized object (and note that session.Merge returns another object that you must then use!)

For example:

deserializedObject = session.Merge(deserializedObject);

From this point on, the deserializedObject is the object that NHibernate is tracking.

Remember to assign the result of session.Merge!

The next line is totally useless and is a common error!

session.Merge(deserializedObject);

Upvotes: 1

Related Questions