NHibernate:仅加载基类对象 [英] NHibernate: Load base class objects only

查看:123
本文介绍了NHibernate:仅加载基类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎任何形式的帮助。即使你可以说(根据你的经验),使用ORM这样一个巨大的层次结构是疯了:)

Any kind of help is welcome. Even if you can say (based upon your experience) that using an ORM for such a huge hierarchy is insane :).

背景
我的模型层有一个非常巨大的类层次结构,即有大约200个类。层次结构的好/坏事是它们都有相同的基类。基类和叶类之间的最大距离为7,层次结构中任何级别上的最大数字类为80.
我使用nHibernate从持久存储中保存/加载数据。

Backgroud My model layer has a pretty huge class hierarchy i.e. there are around 200 classes. The good/bad thing with hierarchy is that all of them have the same base class. The maximum distance between the base and leaf classes is 7 and the maximum number classes at any level in hierarchy is 80. I am using nHibernate to save/load data from persistent storage.

问题
nHibernate生成的查询效率非常高。例如,如果我想基于对基类中的一个属性的一些过滤器来选择对象的id,NHibernate将尝试连接层次中的所有表/联合它们,取决于我选择的映射策略,即表每个子类或每个类的表

Problem The queries generated by nHibernate are pretty in efficient. e.g if I want to select ids of objects based upon some filter on a property in the base class, NHibernate will try to join all the tables in hierarchy/Union them depending which mapping strategy do I choose i.e. table per sub class or table per class hierarchy.

我知道nHibernate不会扫描所有相关的表,直到它的类型。但是如果我现在只对基类数据感兴趣呢。如何强制nHibernate只加载基类对象。

I understand that nHibernate does not which type of object until it can scan all the relevant tables. But what if I am only interested in the base class data at the moment. How to force nHibernate to load only the base class objects.

为了说明我的问题,这里是一个简化的版本

To illustrate my problem, here is a simplified version

public class Vehicle
{
    public virtual Guid Identifier { get; set; }
    public virtual int WheelsCount { get; set; }
    public virtual Make Make { get; set; }
    public virtual Model Model { get; set; }
}

public class Bike : Vehicle
{
    public Bike()
    {
        WheelsCount = 2;
    }

    public virtual bool IsDirtBike { get; set; }
}

public class Car : Vehicle
{
    public Car()
    {
        WheelsCount = 4;
    }

    public virtual bool IsFourWheelDrive { get; set; }

    public virtual string Title { get; set; }
    public virtual string Description { get; set; }
}

public class Make
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Model> Models { get; set; }
}

public class Model
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Make Make { get; set; }
}

并且映射如下

public class VehicleMap : ClassMap<Vehicle>
{
    public VehicleMap()
    {
        Id(x => x.Identifier).GeneratedBy.Guid();
        Map(x => x.WheelsCount);

        References(x => x.Make).Column("MakeId");
        References(x => x.Model).Column("ModelId");

        Table("Vehicle");
        Polymorphism.Explicit();
        UseUnionSubclassForInheritanceMapping();
    }
}

public class BikeMap : SubclassMap<Bike>
{
    public BikeMap()
    {
        Map(x => x.IsDirtBike);
        Table("Bike");
        // Abstract();
    }
}

public class CarMap : SubclassMap<Car>
{
    public CarMap()
    {
        Map(x => x.Title);
        Map(x => x.Description);
        Map(x => x.IsFourWheelDrive);
        Table("Car");
       // Abstract();
    }
}

public class MakeMap : ClassMap<Make>
{
    public MakeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        HasMany(x => x.Models)
            .KeyColumn("MakeId");
        Table("Make");
    }
}

public class ModelMap : ClassMap<Model>
{
   public ModelMap()
   {
       Id(x => x.Id);
       Map(x => x.Name);
       References(x => x.Make)
           .Column("MakeId");
       Table("Model");
   }
}

现在,如果运行以下查询以加载四轮车辆,NHibernate会加入车辆,汽车和自行车桌。现在我只需要存储在Vehicle表中的数据

Now if run the following query to load four wheeled vehicles, NHibernate will join vehicles, car and bike table. Whereas all I need right now is only the data stored in Vehicle table

List<Vehicle> vehicles = session.Query < Vehicle > ().Where(v => v.WheelsCount > 2).ToList();

有没有人知道如何强制nHibernate加载当前需要的数据,只有车辆物体而不是汽车/自行车?在你的模式只有几个表,你可以忽略这些查询nHibernate,但它真的伤害当你有200个表:(。

Does anyone know how can I force nHibernate just load the data the is currently needed i.e. if it can return only vehicle objects instead of Car/Bike? With just a couple of tables in you schema you can overlook these queries by nHibernate but it really hurts when you have 200 tables :(.

PS如果有错误与模型,请忽略它,这不是真正的模型,前面提到的实际模型是更大的,这个模型有说明问题。

P.S. In case there is a fault with model, please ignore that. This is not the real model. The actual model as stated earlier is much bigger. This model is there to illustrate the problem.

推荐答案

NHibernate必须连接表来决定返回哪个类型,否则多态会被破坏,而且处理egde类的情况比较难处理。

NHibernate has to join the tables to decide which type to return. otherwise polymorphism would be broken. Also it would be much harder to handle egde cases like abstract base class and so on.

只投资你需要的属性,而且你还是好的

Project only the properties you need and you are good to go

var vehicledatas = session.Query<Vehicle>()
    .Where(v => v.WheelsCount > 2)
    .Select(v => new { v.Id, v.WheelCount, v.Price })
    .ToList();



如果你需要absolutly只想基类,它独立于此用例

If you need absolutly want only the base class then map it seperate for this use case

public class AlternateVehicleMap : VehicleMap
{
    public AlternateVehicleMap()
    {
        EntityName("IAbsolutlyWantOnlyTheBaseClass");
        Readonly();   // to make sure noone messes up
    }
}

List<Vehicle> vehicles = session.CreateCriteria<Vehicle>("IAbsolutlyWantOnlyTheBaseClass").Add(Expression.Gt("WheelsCount", 2).List<Vehicle>();

这篇关于NHibernate:仅加载基类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆