开发者

Get subclass type from projection with NHibernate

I am trying to do a projection on a type called Log. Log references a superclass called Company. Each company type is mapped with table per subclass.

Is it possible for me to get the type of Company when I do a projection on Log? I currently have an Enum property (which is not mapped) on each subclass, so I can perform switches on Company types, but since it is not mapped to anything, I can't do a projection on it.

I have tried Projections.Property("log.Company.class") but that does not work :(

PS: I couldn't 开发者_运维知识库find a lot of appropriate tags for this question. If anyone have an idea for more specific tags, please tell me.


You can do the following...

session.CreateCriteria<Log>()
       .CreateAlias("Company", "company")
       .SetProjection(Projections.Property("company.class"))

But that projection can only be used for filtering and ordering; NHibernate will not return the System.Type in the result set (it returns an integer, which is used internally).

If what you need is to know the concrete type of the Company, you can do the following:

var logs = session.CreateCriteria<Log>()
                  .SetFetchMode("Company", FetchMode.Join) //avoid SELECT N+1
                  .List<Log>()

And then, to retrieve the type for each row:

foreach (var log in logs)
    string companyClassName = session.GetEntityName(log.Company);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜