Hi 

With NHibernate 3.0 LINQ I'd like to get all the projects that a certain 
person have access to.  I have 
Project --> PersonProject <-- Person

I want to find all the projects for a given person

This works:
  var projects = (from p in session.Current.Query<Project>()
                  where p.Personprojects.Count(pp => pp.Id.PersonId == 
userId) > 0
                  select p
                  ).ToList();

*Are there any better ways of doing it?* I tried .Any but that didn't work 
on Oracle <https://groups.google.com/d/topic/nhusers/Cmz8u14mISg/discussion>
.

This QueryOver does the same thing, but with a different SQL
            var projects = NHibernateSession.Current.QueryOver<Project>()
                .Right.JoinQueryOver<Personproject>(p => p.Personprojects)
                .Where(pp => pp.Id.PersonId == userId)
                .List();

*Relevant mappings*:
Person.hbm.xml:
    <set name="PersonprojectsForPersonId" inverse="true" lazy="true" 
table="PERSONPROJECT" fetch="select">
      <key>
        <column name="PERSON_ID" precision="10" scale="0" not-null="true" />
      </key>
      <one-to-many class="Personproject" />
    </set>

Personproject.hbm.xml
  <class name="Personproject" table="PERSONPROJECT">
    <composite-id name="Id" class="PersonprojectId">
      <key-property name="ProjectId" type="Int64">
        <column name="PROJECT_ID" precision="10" scale="0" />
      </key-property>
      <key-property name="PersonId" type="Int64">
        <column name="PERSON_ID" precision="10" scale="0" />
      </key-property>
    </composite-id>
    <many-to-one name="ProjectReference" class="Project" update="false" 
insert="false" fetch="select">
      <column name="PROJECT_ID" precision="10" scale="0" not-null="true" />
    </many-to-one>
    <many-to-one name="PersonByPersonId" class="Person" update="false" 
insert="false" fetch="select">
      <column name="PERSON_ID" precision="10" scale="0" not-null="true" />
    </many-to-one>

Project.hbm.xml
    <set name="Personprojects" inverse="true" lazy="true" 
table="PERSONPROJECT" fetch="select">
      <key>
        <column name="PROJECT_ID" precision="10" scale="0" not-null="true" 
/>
      </key>
      <one-to-many class="Personproject" />
    </set>



-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to