I have Canine and CanineHandler objects in my application. The CanineHandler object has a PersonID (which refers to a completely different database), EffectiveDate (which indicates when the handler started with the dog), and an FK link to Canine (CanineID).
Given a specific PersonID, I want to find all the fangs for which they are currently responsible. The (simplified) query that I would use in SQL would be:
Select Canine.* from Canine inner join CanineHandler on(CanineHandler.CanineID=Canine.CanineID) inner join (select CanineID,Max(EffectiveDate) MaxEffectiveDate from caninehandler group by CanineID) as CurrentHandler on(CurrentHandler.CanineID=CanineHandler.CanineID and CurrentHandler.MaxEffectiveDate=CanineHandler.EffectiveDate) where CanineHandler.HandlerPersonID=@PersonID
Edit: Added mapping files below:
<class name="CanineHandler" table="CanineHandler" schema="dbo"> <id name="CanineHandlerID" type="Int32"> <generator class="identity" /> </id> <property name="EffectiveDate" type="DateTime" precision="16" not-null="true" /> <property name="HandlerPersonID" type="Int64" precision="19" not-null="true" /> <many-to-one name="Canine" class="Canine" column="CanineID" not-null="true" access="field.camelcase-underscore" /> </class> <class name="Canine" table="Canine"> <id name="CanineID" type="Int32"> <generator class="identity" /> </id> <property name="Name" type="String" length="64" not-null="true" /> ... <set name="CanineHandlers" table="CanineHandler" inverse="true" order-by="EffectiveDate desc" cascade="save-update" access="field.camelcase-underscore"> <key column="CanineID" /> <one-to-many class="CanineHandler" /> </set> <property name="IsDeleted" type="Boolean" not-null="true" /> </class>
I haven't tried it yet, but I guess I can do it in HQL. I havenβt had to write anything in HQL yet, so I still have to solve it, but my question is whether I can make this subquery with criteria / subquery objects.
I went on to create the following discarded criteria:
DetachedCriteria effectiveHandlers = DetachedCriteria.For<Canine>() .SetProjection(Projections.ProjectionList() .Add(Projections.Max("EffectiveDate"),"MaxEffectiveDate") .Add(Projections.GroupProperty("CanineID"),"handledCanineID") );
but i can't figure out how to make an inner join. If I do this:
Session.CreateCriteria<Canine>() .CreateCriteria("CanineHandler", "handler", NHibernate.SqlCommand.JoinType.InnerJoin) .List<Canine>();
I get the error "could not resolve property: CanineHandler from: OPS.CanineApp.Model.Canine". Obviously, I missed something, but from the documentation I got the impression that I should return a list of Canines that has handlers (possibly with duplicates). Until I can do this work, adding a subquery will not work ...
I found similar questions, such as Get only the latest results using nHibernate , but none of the answers really apply to the direct result view. looking for.
Any help or suggestion is appreciated.