开发者

NHibernate - AliasToBean and Associations

I have an entity which looks like this:

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

The property "Name" is not a mapped property but "Id" and "UserRole" is. I want to populate this class using a custom criteria like this:

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开发者_如何学Python.Desc"), "UserRole.Desc")
                .Add(Projections.Property("r.Permission"), "UserRole.Permission")
    )
    .SetResultTransformer(Transformers.AliasToBean<EntityPermission>());

However if I execute this it throws an exception "Could not find a setter for property '{UserRole.Id}' in class 'MyEntity'".

So my question is doesn't aliastobean support associations, and if it does, what's the proper syntax?


You should crate an alias to "UserRole" instead of "Role", since the name of the property is "UserRole", not "Role".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜