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

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

问题描述

我使用NHibernate的(我是一个完整的小白),而我希望能够做的就是复制一个从数据库加载一个实体​​,并将其保存一个新的ID ......有没有人遇到这种情况?任何帮助将是非常美联社preciated。

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>

如果会话不再知道它检索的对象,也就是说,如果您在保存和冲洗,只是改变了IDS将现在的工作之前,驱逐他们。所以,你可以写code是这样的:

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();


    }

这里的问题是,如果你的对象图有任何深度,你必须确保驱逐整个集合,然后你仍然必须再次检索它们,您可能需要在会议上的原件。这是一个后勤头痛,并产生code非常晦涩难懂的意向。

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.

这是不适合我的情况,我在做什么是我写的手工code键使使用拷贝构造一个对象图的深层副本。这是复杂的,还可以导致维护的问题,但是如果对象不能序列很少有更好的替代品。

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天全站免登陆