NHibernate - AliasToBean和Associations

时间:2010-12-17 10:57:02

标签: nhibernate

我有一个看起来像这样的实体:

public class MyEntity {
 public virtual int Id { get; set; }
 public virtual string Name { get; set ; }
 public virtual Role UserRole { get; set; }
}

属性“Name”不是映射属性,而是“Id”和“UserRole”。我想使用这样的自定义标准填充此类:

ICriteria c = Db.CreateCriteria<MyEntity>("m")
 .CreateAlias("Role", "r")
 .SetProjection(Projections.ProjectionList()
 .Add(
  Projections.SqlProjection(@"Name = case TypeId
                    when 2 then (select Name from tblOne where Id = EntityId)
                    when 4 then (select Name from tblTwo where Id = EntityId)
                    when 3 then (select Name from tblThree where Id = EntityId)
                    end", new string[] { "Name" }, new NHibernate.Type.IType[] { NHibernateUtil.String }))
                .Add(Projections.Property("m.Id"), "Id")
                .Add(Projections.Property("r.Id"), "UserRole.Id")
                .Add(Projections.Property("r.Desc"), "UserRole.Desc")
                .Add(Projections.Property("r.Permission"), "UserRole.Permission")
    )
    .SetResultTransformer(Transformers.AliasToBean<EntityPermission>());

但是,如果我执行此操作,则会抛出异常“无法在”MyEntity“类中找到属性'{UserRole.Id}'的setter。”

所以我的问题是不是aliastobean支持关联,如果确实如此,那么正确的语法是什么?

1 个答案:

答案 0 :(得分:0)

你应该创建一个别名为“UserRole”而不是“Role”,因为该属性的名称是“UserRole”,而不是“Role”。