开发者

NHibernate self referencing query

开发者 https://www.devze.com 2023-01-07 06:33 出处:网络
How do you write this in NHibernate? criteria .CreateAlias( \"CreatorObject.LastCreated\", \"me\" ) .Add( Restrictions.Eq( this, \"me\" ) );

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));
0

精彩评论

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