NHibernate Session.SetReadOnly [英] NHibernate Session.SetReadOnly

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

问题描述

我正面临同样的问题,其他问题已经发布在SO上:从数据库读取对象时,NHibernate将更新所有对象,因为一个字段的值在数据库中不合适.

I'm facing the same issue others already posted on SO: On reading objects from the database, NHibernate would update all the objects because the value of one field is not proper in the DB.

(详细说明:新添加的日期列在所有行中都包含"1/1/0001",因此在映射时,NHibernate会替换日期,并在tx.Commit()上更新每一行.)

(In detail: A newly added date column contains "1/1/0001" in all rows, so on mapping, NHibernate replaces the date and, on tx.Commit(), updates every single row.)

[事实证明这是错误的.相反,这些日期字段为空,但是会被NHibernate更新为1/1/0001.有关详细信息,请参见Diego的答案.]

[ It turned out that this was wrong. Instead, these date fields were null but would be updated to 1/1/0001 by NHibernate. See Diego's answer for details.]

为防止这种情况,我发现了这篇文章加上Ben Scheirman的答案以及博客评论均已提及由OP .

To prevent that, I found this post with an answer by Ben Scheirman as well as the blog comment referred to by the OP.

评论员克里斯蒂安(Christian)说:

The commenter Christian says:

您还可以通过使用session.setReadOnly(o,true)禁用Hibernate中的快照或使用query.setReadOnly(true)禁用所有查询对象的快照来禁用自动脏检查和更新.

You can also disable automatic dirty checking and updating by disabling the snapshots in Hibernate with session.setReadOnly(o, true) or for all queried objects with query.setReadOnly(true).

(请注意,此博客文章是有关Java Hibernate的.)

(Note that this blog post is about Java Hibernate.)

query.SetReadOnly(true)成功.但是,我也有这样的代码:

query.SetReadOnly(true) was successful where I used queries. However, I also have code like this:

ISession session = this.NHibernateHelper.SessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();

try
{
    BO resultBO = session.Get<BO>(id);

    tx.Commit();
    return resultBO;
}
catch (Exception ex)
{
    tx.Rollback();
    throw ex;
}
finally
{
    session.Close();
}

在这种情况下,我没有查询,并且所述Session.SetReadOnly(resultBO, true)在NHibernate中不存在. 哪里去了?

In this case I don't have a query, and the said Session.SetReadOnly(resultBO, true) does not exist in NHibernate. Where has it gone?

我猜逐出"基本上不是一个好主意,因为它使对象成为瞬态,所以我不能用它来在另一个会话中更新值(至少它变得更加复杂.我还需要确保所有对象总是被驱逐,因此我的通用更新方法不需要区分持久对象和临时对象-还是我完全错了?

I guess "evict" is basically not a good idea because it makes the object transient so I can't use it to update values in another session (at least it gets more complicated. I would also need to make sure all objects are always evicted so my generic update methods wouldn't need to differentiate between persistent and transient objects - or am I completely wrong?

谢谢和欢呼, 奇科多罗

Thanks and cheers, chiccodoro

推荐答案

您在攻击症状而不是疾病.

You are attacking the symptom instead of the disease.

您所拥有的是一个幽灵(请参阅 http://jfromaniello.blogspot.com/2010/02/nhibernate-ghostbuster-version-11.html )

What you have there is a ghost (see http://jfromaniello.blogspot.com/2010/02/nhibernate-ghostbuster-version-11.html)

将该属性设置为可空,就可以了.

Make the property nullable and you'll be fine.

这篇关于NHibernate Session.SetReadOnly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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