NHibernate的 - 访问相关对象的ID不懒加载整个对象 [英] NHibernate - access the ID of an associated object without lazy loading the whole object

查看:97
本文介绍了NHibernate的 - 访问相关对象的ID不懒加载整个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相关的业务对象 - A和B.
该协会(A-> B)多到之一,B.Id在A(所以A有A.B_id在DB)。外键

I have two associated business objects - A and B. the association is (A->B)many-to-one, with B.Id a foreign key in A (so A has A.B_id in the DB).

我使用的是懒惰= TRUE,解决了我的大部分问题,
然而,在的ToString我也想打印A.B.Id,我应该有没有进一步地去DB。但访问A·B激活代理,并且因为这不是在打开的会话的情况下,抛出异常。

I'm using lazy=true and solved most of my problems, however in A's ToString I want to print also A.B.Id, which I should have without further trips to the DB. but accessing A.B activates the proxy, and since this isn't in the context of an open session, throws an exception.

一个简单而丑陋的解决方案是将有A.B_id财产。但是这是我们试图避免在首位的东西的一部分。
任何有机的方式来做到这一点? :)
谢谢!

one easy but ugly solution would be to have A.B_id property. but that's part of the stuff we were trying to avoid in the first place. any "organic" way to do this? :) thanks!


更新:刚刚看了一下缓存和Session.Get与Session.Load。之前我只新一个抛出,如果对象不存在异常(Session.Load),另一个返回一个空对象(Session.Get)。读到缓存<一后href=\"http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/09/first-and-second-level-caching-in-nhibernate.aspx\"相对=nofollow>这里,很明显,Session.Load返回一个代理对象,只懒洋洋地获取它的时候比其他ID属性被访问,这很像是我从联想需要!
现在我加入了单独的对象标识(添加B_ID到A,所以我可以访问它A.B_Id而不是使用A.B.Id)

UPDATE: just read about caching and Session.Get vs. Session.Load. before I only new that one throws an exception if the object doesn't exist (Session.Load), and the other returns a null object (Session.Get). after reading about caching here, it's clear that Session.Load returns a proxy to the object, and only lazily fetches it when a property other than the ID is accessed, which is very much like what I need from associations! for now I added the separate object ids (added B_Id to A so I can access it as A.B_Id instead of using A.B.Id)

推荐答案

有关我用我所有的多到一的关系,明确A·B-ID属性完全相同的原因。我不认为这是一个快速和肮脏的解决方案,它提供了一个解决这个问题,也是很大的灵活性是储蓄 - 更新区域,即我并不需要从数据库对象B获取只是将其分配到A为了创建关联的时候我有一个查询字符串或其他地方的B_ID。

For the exact same reason I have used explicit A.B-ID properties for all my many-to-one relationships. I do not see this as a quick and dirty solution as it provides a solution to this problem and also lot of flexibility is the saving-updating area i.e. I do not need to fetch from the database the B object just to assign it to A in order to create the association when I have the B_ID in a query string or somewhere else.

我的映射文件useually是这样的:

My mapping files useually look like this:

<property name="CreatorID" column="CreatorID" type="Int32" not-null="true" />
<many-to-one name="Creator" column="CreatorID" class="SystemUser" insert="false" update="false" cascade="none" />

正如你可以看到2属性已设置为只读,以避免NHibernate的发送2次此列到数据库时插入或updatas正在发生。以上使得为只读(通过使用插入=假更新=false的属性)许多对一,但你可以代替有作为只读属性的CreatorID如果你喜欢。

As you can see one of the 2 properties has to be read only to avoid having NHibernate sending 2 times this column to the database when inserts or updatas are happening. The above makes as read only (by using the insert="false" update="false" attributes) the many-to-one but you can instead have as read only the CreatorID property if you like.

仅仅具有许多到一个你没有在实体A类持有B.ID值的属性。只有这样,才能得到它是通过访问对象B将触发代理,它会触发一个数据库查询(如果没有在会议上加载的话)。

Having only the many-to-one you do not have a property in your entity class A to hold the B.ID value. The only way to get it is by accessing the B object which will trigger the proxy and it will fire a query to the database (if it is not loaded in the session already).

我会很高兴地听到,提供了解决方案,并提供同类灵活性,任何其他选项。

I will be happy to hear any other option that provides a solution and offers the same kind of flexibility.

这篇关于NHibernate的 - 访问相关对象的ID不懒加载整个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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