如何使用 NHibernate 复制对象 [英] How do I copy an object with NHibernate

查看:27
本文介绍了如何使用 NHibernate 复制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nhibernate(我是一个完整的菜鸟),我希望能够做的是复制从数据库加载的实体并使用新的 Id 保存它......有没有人遇到过这种情况?任何帮助将不胜感激.

I am using Nhibernate (I am a complete noob), and what I want to be able to do is copy an entity that is loaded from the database and save it with a new Id... has anyone run into this situation? Any help would be very appreciated.

推荐答案

我需要为一组非常复杂的对象执行此操作,到目前为止我发现的是:

I need to do exactly this for a very complex set of objects and what I have found so far is:

NHibernate 并不完全支持这一点.

NHibernate does not exactly support this.

如果您尝试简单地替换从会话中获得的对象的 ID,您将收到 Nhibernate 错误:实例的标识符已从 <9ae3868d-17bf-4314-ba0c-4eb3b44b1a2e> 更改为 <2b2b67c6-a421-48c4-836c-4c27f6481718>

If you try to simply replace the Id of an object you got from a session, you will get an Nhibernate error: identifier of an instance of was altered from <9ae3868d-17bf-4314-ba0c-4eb3b44b1a2e> to <2b2b67c6-a421-48c4-836c-4c27f6481718>

如果会话不再知道它检索到的对象,即如果您在保存和刷新之前驱逐它们,现在只需更改 id 即可.所以你可以写这样的代码:

If the session no longer knows about the objects it retrieved, i.e if you evict them before saving and flushing, just changing the ids will now work. So you could write code like this:

 public void CloneStudent(Guid studentId)
    {
        // Get existing student
        Student student = _session.Get<Student>(studentId);

        // Copy by reference
        Student newStudent = student;

        // Reset Id to do quick and dirty clone
        newStudent.Id = Guid.NewGuid();
        newStudent.Sticker = "D";

        // Must evict existing object or Nhibernate will throw object modified error
        _session.Evict(student);

        // Save new object
        _session.Save(newStudent);
        _session.Flush();


    }

这样做的问题是,如果您的对象图有任何深度,您必须确保驱逐整个集合,然后您可能需要会话中的原件,但您仍然必须再次检索它们.这是一个令人头疼的后勤问题,并且会产生具有非常模糊和复杂意图的代码.

The problem with this is if your object graph has any depth you have to be sure to evict the entire set, and then you may need the originals in the session still you have to retrieve them again. This is a logistical headache and yields code with very obscure and convoluted intentions.

我不推荐.

更常见的做法是将二进制流序列化,并将该流重组为一组新的对象.很好,但只有在您的对象都是可序列化的情况下才有效.

What is more commonly done is serialize to a binary stream and reconstitute this stream into a new set of objects. Fine, but only works if your objects are all serializable.

对我来说不是这样,我正在做的是我编写手动代码来使用复制构造函数制作对象图的深层副本.这很复杂,也可能导致维护问题,但如果对象无法序列化,则几乎没有更好的选择.

That is not the case for me, what I am doing is I wrote manual code to make deep copies of an object graph using copy constructors. This is complex and also can lead to maintenance issues, but if the objects cannot be serialized there are few better alternatives.

抱歉,如果不能选择序列化,那么深度复制对象仍然是一项复杂的任务.

Sorry, deep copying objects remains a complicated task if serialization is not an option.

这篇关于如何使用 NHibernate 复制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆