提取没有主键NHibernate的孩子 [英] Fetch child without primary key NHibernate

查看:105
本文介绍了提取没有主键NHibernate的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过映射将对象集合变成父对象.

I am trying to get a collection of objects into a parent object through mapping.

我有一个主对象为GUID(Id)的父对象"ScoreCard"和一个主键为GUID(Id)的子"Score"对象.我想根据两个对象都具有的两个字段为父对象选择子对象,但我无法使其正常工作,这是映射

I have a parent object "ScoreCard" whose primary key is a guid (Id) and a child "Score" object whose primary key is a guid (Id). I want to select the child objects for the parent based on two fields that both objects have but I can't get it to work, here's the mapping

<bag name="ScoreCard">
  <key>
    <column name="HoleId"/>
    <column name="PlayerId"/>
  </key>
  <one-to-many class="Score" not-found="ignore"/>
</bag>

我没有设计数据库,但是ScoreCard对象来自一个视图,该视图返回我需要的两列以及邪恶的guid.无论我尝试了什么,NHibernate都会不断抛出一个关于外键与主键不同的异常.

I didn't design the database but the ScoreCard object comes from a view that returns both column I need plus the evil guid. Whatever I've tried, NHibernate keeps throwing an exception about the foreign key not being the same as the primary key.

在我看来,这是最简单的要求,在给出一些标准的前提下收集了一些东西,为什么我会如此困住?

This seems to me to be the most simple requirement, get a collection of things given some criteria, why am I so stuck?

感谢您的帮助,对于错误的示例代码(在亲戚家观看潜艇的情况)表示抱歉.

Thanks for your help, sorry for the bad example code (subliminal golf watching at relatives house).

推荐答案

好吧,我最终找到了它.父对象是从提供三列且没有键的视图绘制的.我可以将组合键映射到HoleId和PlayerId,而不是在查看代码时发现的邪恶GUID.很棒,因为我可以轻松映射所需的Score对象,然后使用NHibernateUtil.Initialize延迟加载它们.

Well, I found it eventually. The parent object is drawn from a view giving three columns and no key. I can map a composite key to the HoleId and PlayerId instead of the evil guid that I found when I looked at the code. This is great as I can easily map the Score objects I need and then lazy load them using NHibernateUtil.Initialize.

我的映射XML必须看起来像这样

My mapping xml needs to look like this

<class name="ParentObject">
    <composite-id>
      <key-property name="HoleId" column="HoleId" />
      <key-property name="PlayerId" column="PlayerId" />      
    </composite-id>
    <property name="EvilGuid" column="Id" />
    <bag name="ScoreCard">
      <key>
        <column name="HoleId"/>
       <column name="PlayerId"/>
      </key>  
      <one-to-many class="Score" not-found="ignore"/>
    </bag>
</class>

我从这篇文章中得到了启发,请另外支付请注意Stefan的答案,因为我觉得自己在这里很幸运,可以通过对DDD的更多考虑来改进设计.

I got my inspiration from this post, please also pay attention to Stefan's answer as I feel I had a lucky break here, and the design could be made better with more thought about DDD.

感谢您的帮助.

这篇关于提取没有主键NHibernate的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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