views:

163

answers:

1

Hi

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.

+1  A: 

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);
Diego Mijelshon
Is that predictable? I mean, can I translate that to a string, which basically is what I need, to display the type of company?
TigerShark
It can be predictable, but only by reverse engineering. I'll add an alternative for what you need.
Diego Mijelshon
That looks nice, but wouldn't it be a join no matter what, as I'm using table per subclass?I'll give it a shot :)
TigerShark
Well it works. I just need to know whether I can use the integers as a reference, or I have to go through the session to translate the them to the correct classname. I'm using AliasToBeanConstructor transformer, in which I translate the integer to my own displayname. But if I need to use the session to translate it, I need to change my approach.
TigerShark
When I have that kind of complex projection, instead of using AliasToBean I use .List<object[]> and then I do the mapping with Linq-to-objects. BTW, if this helped you it's usually nice to accept the answer...
Diego Mijelshon
Your answer is accepted (had a busy weekend) :)But, is there any way I can tell whether the integers would be predictable (ie. static)? Are they generated based on the index position of a list of sorted classnames?Or maybe I should that that one up with the mailinglist..?
TigerShark
They are probably based either on hbm order or class name, yes. You should either check the NH sources or (probably faster) create a small proof of concept.
Diego Mijelshon
After doing some tests, it seems like the values for the classes are predictable. I've tried renaming the classes etc, but it didn't change the order. I just worry that it's not safe, so I might just do as you say, and use object[] instead.
TigerShark