简体   繁体   English

提取没有主键NHibernate的孩子

[英]Fetch child without primary key NHibernate

I am trying to get a collection of objects into a parent object through mapping. 我正在尝试通过映射将对象集合变成父对象。

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). 我有一个主对象是GUID(Id)的父对象“ ScoreCard”和一个主键是GUID(Id)的子“ Score”对象。 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>

I didn't design the database but the ScoreCard object comes from a view that returns both column I need plus the evil guid. 我没有设计数据库,但是ScoreCard对象来自一个视图,该视图返回我需要的两列以及邪恶的guid。 Whatever I've tried, NHibernate keeps throwing an exception about the foreign key not being the same as the primary key. 无论我尝试了什么,NHibernate都会不断抛出关于外键与主键不同的异常。

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). 感谢您的帮助,对于错误的示例代码(在亲戚家中进行潜意识高尔夫观看)表示抱歉。

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. 我可以将组合键映射到HoleId和PlayerId,而不是在查看代码时发现的邪恶GUID。 This is great as I can easily map the Score objects I need and then lazy load them using NHibernateUtil.Initialize. 很棒,因为我可以轻松映射所需的Score对象,然后使用NHibernateUtil.Initialize延迟加载它们。

My mapping xml needs to look like this 我的映射XML需要看起来像这样

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

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. 我从这篇文章中得到了启发,也请注意Stefan的回答,因为我觉得自己在这里度过了一个幸运的假期,可以通过对DDD的更多思考来改进设计。

Thanks for your help. 谢谢你的帮助。

The problem is this: NHibernate works best (but not only) for DDD, this means for creating domain classes first and make the database best fitting the domain model. 问题是这样的:NHibernate对DDD最好(但不仅如此)工作,这意味着首先创建域类,并使数据库最适合域模型。

You have a composite-id relation to non-primary-key fields. 您与非主键字段具有复合ID关系。 So start praying that NHibernate can cope with that. 因此,开始祈祷NHibernate可以解决这个问题。 Both composite-ids and relations by non-primary-keys are supported - for legacy databases - and generally discouraged for DDD. 对于旧数据库,复合ID和非主键之间的关系均受支持,而DDD通常不建议使用。

I think the combination of both does not work. 我认为两者的结合是行不通的。 See this issue on NHibernates issue tracker: https://nhibernate.jira.com/browse/NH-1722 . 在NHibernates问题追踪器上查看此问题: https : //nhibernate.jira.com/browse/NH-1722 You can vote for the feature there. 您可以在此处对该功能进行投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM