views:

30

answers:

1

I'm trying to create Class Table Inheritance as in ( http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html )

So let's say I have 2 classes:

[ActiveRecord("entity"), JoinedBase]
public class Entity : ActiveRecordBase
{
  private int id;
  private string name;
  private string type;

  ...and properties
}

[ActiveRecord("entitycompany")]
public class CompanyEntity : Entity
{
  private byte company_type;
  private int comp_id;

  [JoinedKey("comp_id")]
  public int CompId
  {
    get { return comp_id; }
    set { comp_id = value; }
  }
}

It seems to be ok, all tests are fine. One thing I see in profiler and it drives me mad is that if Entity is used (it's a property) in some other class (let's call it World), then fetching World results in left outer join on both Entity and CompanyEntity. I would expect it to be just a join on Entity!

Can anyone help me with that and explain why is it happening?

+1  A: 

ActiveRecord (actually NHibernate) has to do a left join on CompanyEntity because it has to know what type the Entity really is. Without left joining on CompanyEntity it can't possibly know if it's a CompanyEntity or not so it wouldn't know what to instantiate.

In other words, without this left join your class hierarchy would break.

It doesn't really cause any significant performance issues, but if it bothers you that much try switching to another inheritance strategy, like discriminator.

Mauricio Scheffer
good one, thanks!!Can't switch to discriminator because my children objects all rely on the same column in the parent - I think it eliminates possibility of discriminator.
StupidDeveloper