开发者

One-to-many NHibernate

hello I have mapping like this :

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

I need fetch all persons that live in Paris. For this I use query like select p from Person p inner join Address a on p.Id=a.PersonId where a.City like 'Paris' And it's O.k. But Nhbernate Executes another one query , select a from Address a where a.PersonId in (all ids of persons that live in paris) but it's unnecessary , mountains NHibernate can get all fields of Address from join(fir开发者_开发百科st query)

Can I Prevent running the second query and get all needed information from first query ????


Yes you can, you just need to specify a fetch. This will initialize the addresses collection.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜