NHibernate - 无需延迟加载整个对象即可访问关联对象的 ID [英] NHibernate - access the ID of an associated object without lazy loading the whole object

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

问题描述

我有两个关联的业务对象 - A 和 B.关联是 (A->B) 多对一,B.Id 是 A 中的外键(所以 A 在数据库中有 A.B_id).

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).

我使用了 lazy=true 并解决了我的大部分问题,但是在 A 的 ToString 中,我还想打印 A.B.Id,我应该在没有进一步访问数据库的情况下打印它.但是访问 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).在阅读关于缓存这里,很明显,Session.Load 返回的是一个对象的代理,只有在访问ID以外的属性时才懒惰地获取它,这很像我需要从关联中获取的!现在我添加了单独的对象 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.

我的映射文件通常如下所示:

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 次到数据库.以上使多对一成为只读(通过使用 insert="false" update="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天全站免登陆