【问题标题】:One-to-many NHibernate一对多 NHibernate
【发布时间】:2011-05-23 01:51:08
【问题描述】:

你好 我有这样的映射:

<class entity-name="Person">
  <id name="id" type="long" column="ID"> 
    <generator class="sequence"/>
 </id>      
<property name="FirstName" column="FIRST_NAME" type="string"/> 
<property name="LastName" column="LAST_NAME" type="string"/> 
<bag name="Addresses" inverse="true" lazy="false"         cascade="all">         <key column="Person_ID"/>        
<one-to-many class="Address"/>     </bag>  

<class entity-name="Address">
  <id name="id" type="long" column="ID"> 
    <generator class="sequence"/>
 </id>      
<property name="City" column="City" type="string"/> 
<property name="Country" column="Country" type="string"/> 
<property name="PersonId" column="Person_ID" type="long"/> 
</class>

我需要找到所有住在巴黎的人。为此,我使用类似的查询 select p from Person p inner join Address a on p.Id=a.PersonId where a.City like 'Paris' 没关系。 但是 Nhbernate 执行另一个查询,从 Address a where a.PersonId in 中选择一个(所有住在巴黎的人的 id) 但没必要,山NHibernate可以从join(first query)中获取Address的所有字段

我可以阻止运行第二个查询并从第一个查询中获取所有需要的信息吗????

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

    是的,你可以,你只需要指定一个提取。这将初始化地址集合。

    http://www.nhforge.org/doc/nh/en/index.html#queryhql-joins

    select p from Person p inner join fetch 
    Address a on p.Id=a.PersonId where a.City like 'Paris'
    

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多