NHibernate self referencing query
How do you write this in NHibernate?
criteria
.CreateAlias( "CreatorObject.LastCreated", "me" )
.Add( Restrictions.Eq( this, "me" ) );
Edit: something like this without using sql
Where there are two tables TypeA and TypeB where typeB creates typeA objects and keeps a reference to the last object created.
criteria
.Add( Restrictions.IdEq( Projections.SqlProjection( "(Select LastCreated From Creators Where Creators.Id = Cr开发者_C百科eatorId) as MasterId", new[] { "MasterId" }, new[] { NHibernateUtil.Int32 } ) ) );
So if you have something like
public class SomeClass {
public SomeClass LastCreated { get; set;}
public SomeClass CreatorObject {get; set;}
}
you could write something like:
criteria.Add(Restrictions.Eq("CreatorObject.LastCreated", this));
精彩评论