views:

282

answers:

1

Hi! I have a (several actually) relationship between two objects, the parent object is a User and the child object is an Incident. There are four relationships defined between User and Incident:

  1. Property User_created of Incident refers to a User who created the Incident
  2. Property User_modified who modified the Incident
  3. User_reported and finally
  4. User_responsible

All these four properties of Incident references a User object. Like this in the mapping:

<many-to-one name="User_created" class="User, WebbData" fetch="select">
    <column name="created_by" not-null="true" />
</many-to-one>
<many-to-one name="User_modified" class="User, WebbData" fetch="select">
  <column name="modified_by" not-null="false" />
</many-to-one>
<many-to-one name="User_reported" class="User, WebbData" fetch="select">
  <column name="reported_by" not-null="true" />
</many-to-one>
<many-to-one name="User_responsible" class="User, WebbData" fetch="select">
 <column name="responsible" not-null="false" />
</many-to-one>

I want to write a nHibernate expression to fetch all Incidents created by a specific user but I don't know how to write this expression... This is what I got so far:

 return session.CreateCriteria(typeof(Incident))
     .CreateCriteria("User")
         .Add(Expression.Eq("Id", UserID))
     .List<Incident>();

but how the heck do I tell nHibernate that it should use the User_created relation and not one of the other three?

A: 

I think this works, with the direct assignment of the ID to an entity property:

 return session.CreateCriteria(typeof(Incident))
     .Add(Expression.Eq("User_created", UserID))
     .List<Incident>();
David M
it does work! so easy I almost feel embaressed I didn't think of it myself... :Sthanks a bunch!
RickardN
You're welcome. I always feel like I'm cheating equating an entity to an ID value like this, but obviously it's totally valid...
David M