Castle Active Record NHibernate Same Session 同一对象的2个版本

发布于 2025-01-08 17:32:57 字数 909 浏览 1 评论 0原文

我在数据库中有一辆汽车,其中模型字段是“法拉利”,

using (new TransactionScope())
{
    var car = Find(1);
    car.Model = "Ferrari Plus";

    // i need the old Car value to make a comparison
    var car2 = Find(1);

    // i need here the db Car record, instead i have the cached Nhibernate value
    if (car2.Model == 'Ferrari") 
        // do something
}

我以这种方式修改了代码,

using (new TransactionScope())
{
    var car = Find(1);
    car.Model = "Ferrari Plus";

    // i need the old Car value to make a comparison
    using (new TransactionScope())
    {
        var car2 = Find(1);
        // i got  the db value buuuuuuut
        // if i save because i need to save the modific Car i got an error
        if (car2.Model == 'Ferrari") 
            car.Save(); // ERROR: 2 objects with the same id exists
    }

}

如何避免这个问题? 如何将同一对象的 2 个版本放入同一个 NHibernate 会话中?

I have a Car in DB where Model field is "Ferrari"

using (new TransactionScope())
{
    var car = Find(1);
    car.Model = "Ferrari Plus";

    // i need the old Car value to make a comparison
    var car2 = Find(1);

    // i need here the db Car record, instead i have the cached Nhibernate value
    if (car2.Model == 'Ferrari") 
        // do something
}

i modified the code in this way

using (new TransactionScope())
{
    var car = Find(1);
    car.Model = "Ferrari Plus";

    // i need the old Car value to make a comparison
    using (new TransactionScope())
    {
        var car2 = Find(1);
        // i got  the db value buuuuuuut
        // if i save because i need to save the modific Car i got an error
        if (car2.Model == 'Ferrari") 
            car.Save(); // ERROR: 2 objects with the same id exists
    }

}

how can i avoid this problem?
How can i get 2 versions of the same object into the same NHibernate Session?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2025-01-15 17:32:57

nhibernate 的基本原则之一是在同一个会话中不要有 2 个相同的对象。据我所知,你所要求的事情无法完成。

可能的解决方案 1

您可以在一个会话中查询 car 关闭会话。在另一个会话中查询 car2 并与两个对象进行比较。使用 car 的属性更新 car2

可能的解决方案 2

执行与上述相同的操作,但不要将值从 car 复制到 car2。相反,您可以打开另一个会话并执行 session.Update(car);

可能的解决方案 3

您可以做的是创建原始会话的副本或创建 dto。当您进行比较时,将持久实例(更改的副本)与副本/dto 进行比较。

One of the base principles of nhibernate is to not have 2 objects that are the same in the same session. What you are asking cannot be done as far as I know.

Possible Solution 1

You could query car in one session close the session. Query car2 in another session and do your compare against the two objects. Update car2 with car's properties.

Possible Solution 2

Do the same as above except don't copy the values from car to car2. Instead you would open another session and do a session.Update(car);

Possible Solution 3

You could do is to create a copy of the original or create a dto. When you do your comparison compare the persistent instance (changed copy) to the copy/dto.

晚风撩人 2025-01-15 17:32:57

您可以 Evict() 一个对象并告诉 ActiveRecord / NHibernate 不再跟踪该对象。但您需要在开始更改之前执行此操作。所以你的代码应该是这样的:

// i need the old Car value to make a comparison
var car2 = Find(1);
ActiveRecordMediator.Evict(car2); //Tell AR to no longer track this object

var car = Find(1);  // now returns a new fresh version from the database
car.Model = "Ferrari Plus";

if (car2.Model == 'Ferrari") 
    // do something

看看这个 Active Record 生命周期图

You can Evict() an object and tell ActiveRecord / NHibernate to no longer track this Object. But you need to do this before you start changing it. So your code should look like this:

// i need the old Car value to make a comparison
var car2 = Find(1);
ActiveRecordMediator.Evict(car2); //Tell AR to no longer track this object

var car = Find(1);  // now returns a new fresh version from the database
car.Model = "Ferrari Plus";

if (car2.Model == 'Ferrari") 
    // do something

Take a Look at this Active Record Lifecycle Diagram.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文