开发者

NHibernate: Is it possible to add restriction based on alias name

开发者 https://www.devze.com 2022-12-13 10:54 出处:网络
Say I create a criteria que开发者_开发问答ry and transform it with the AliasToBean result transormer.

Say I create a criteria que开发者_开发问答ry and transform it with the AliasToBean result transormer.

session.CreateCriteria<Staff>("s")
  .CreateAlias("supervisor", "super")
  .SetProjection(Projections.ProjectionList()
    .Add(Projections.Property("s.name"), "name")
    .Add(Projections.Property("super.name"), "supervisor_name"))
  .SetResultTransformer(Transformers.AliasToBean<StaffView>());

Is it possible to add a restriction when you only know the property name of the bean? You can AddOrder using the bean property names but the Restrictions only appear to work with the actual property name. I'm guessing this is because they are applied before the transformation, much like you don't use a column alias in a where clause but you can in the order by.

//throws exception like cannot find property supervisor_name in Staff class
//only "super.name" works
.Add(Restrictions.Like("supervisor_name", "blah"))

//this works though
.AddOrder(Order.Asc("supervisor_name")) 

So basically I need a way to get the aliased property name based on the alias. I can think of multiple ways to do this, but I was hoping NHibernate had some existing method? I could for example keep a separate map of the property to alias names or just map to a database view so there is no need to transform to bean.


It should work with name of alias dot property name. {alias}.Name.

Or am I misunderstanding your problem?

0

精彩评论

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