Castle ActiveRecord 类表继承
我正在尝试创建类表继承,如 ( http:// www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html )
所以假设我有 2 个类:
[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; }
}
}
看起来没问题,所有测试都很好。我在探查器中看到的一件事让我发疯,如果在其他类(我们称之为 World)中使用 Entity(它是一个属性),那么获取 World 会导致 Entity 和 CompanyEntity 上的左外连接。 我希望它只是实体的加入!
谁能帮助我并解释为什么会发生这种情况?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ActiveRecord(实际上是 NHibernate)必须对 CompanyEntity 执行左连接,因为它必须知道实体的实际类型。如果没有离开 CompanyEntity,它就不可能知道它是否是 CompanyEntity,因此它不知道要实例化什么。
换句话说,如果没有这个左连接,你的类层次结构就会被破坏。
它实际上并不会导致任何重大的性能问题,但如果它让您感到困扰,请尝试切换到另一种继承策略,例如鉴别器。
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.