nHibernate 从子子类中预先加载关系

发布于 2024-11-06 09:23:59 字数 1904 浏览 5 评论 0原文

假设我有以下类定义:

public class Order
{
    public virtual int Id { get; protected set; }
    public virtual IList<OrderItemBase> OrderItems { get; set; }
}

public class OrderItemBase
{
    public virtual int Id { get; protected set; }
    public virtual string OrderRef { get; set; }
}

public class ProductOrder : OrderItemBase
{
    public virtual Product Product { get; set; }
}

public class OtherOrder : OrderItemBase
{
    public virtual string MiscData { get; set; }
}

public class Product
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
}

映射如下:

public class OrderMap : ClassMap<Order>
{
    this.Id(x => x.Id);
    this.HasMany(x => x.OrderItems).Inverse().KeyColumn("OrderId").Cascade.All();
}

public class OrderItemMap : ClassMap<OrderItembase>
{
    this.Id(x => x.Id);
    this.Map(x => x.OrderRef);
    this.DiscriminateSubClassesOnColumn("OrderTypeId");
}

public class ProductOrderMap : ClassMap<ProductOrder>
{
    this.DiscriminatorValue(1);
    this.References(x => x.Product).Column("ProductId");
}

public class OtherOrderMap : ClassMap<OtherOrder>
{
    this.DiscriminatorValue(2);
    this.Map(x => x.MiscData);
}

public class ProductMap : ClassMap<Product>
{
    // ... can't be arsed to type more code in here, you get the idea
}

现在提出问题:

当我为订单存储库编写查询时,我需要一些方法来预先加载所有OrderItems:

return this.All()
    .Where(o => o.Id == id)
    .FetchMany(o => o.OrderItems)

以及一些还加载相关 ProductOrder 产品项目的方法:

return this.All()
    .Where(o => o.Id == id)
    .FetchMany(o => o.OrderItems) // Ok so far...
    .ThenFetch(oi => oi.Product) // Uh-oh, the base order item doesn't have Product

如何让子类项目立即加载其关系,但仅在我需要时才加载?

Say I have the following class definitions:

public class Order
{
    public virtual int Id { get; protected set; }
    public virtual IList<OrderItemBase> OrderItems { get; set; }
}

public class OrderItemBase
{
    public virtual int Id { get; protected set; }
    public virtual string OrderRef { get; set; }
}

public class ProductOrder : OrderItemBase
{
    public virtual Product Product { get; set; }
}

public class OtherOrder : OrderItemBase
{
    public virtual string MiscData { get; set; }
}

public class Product
{
    public virtual int Id { get; protected set; }
    public virtual string Name { get; set; }
}

With mappings along the lines of:

public class OrderMap : ClassMap<Order>
{
    this.Id(x => x.Id);
    this.HasMany(x => x.OrderItems).Inverse().KeyColumn("OrderId").Cascade.All();
}

public class OrderItemMap : ClassMap<OrderItembase>
{
    this.Id(x => x.Id);
    this.Map(x => x.OrderRef);
    this.DiscriminateSubClassesOnColumn("OrderTypeId");
}

public class ProductOrderMap : ClassMap<ProductOrder>
{
    this.DiscriminatorValue(1);
    this.References(x => x.Product).Column("ProductId");
}

public class OtherOrderMap : ClassMap<OtherOrder>
{
    this.DiscriminatorValue(2);
    this.Map(x => x.MiscData);
}

public class ProductMap : ClassMap<Product>
{
    // ... can't be arsed to type more code in here, you get the idea
}

Now for the question bit:

When I come to write a query for the order repository, I want some methods to eager-load all the OrderItems:

return this.All()
    .Where(o => o.Id == id)
    .FetchMany(o => o.OrderItems)

And some methods to also load the related ProductOrder Product items:

return this.All()
    .Where(o => o.Id == id)
    .FetchMany(o => o.OrderItems) // Ok so far...
    .ThenFetch(oi => oi.Product) // Uh-oh, the base order item doesn't have Product

How can I get a sub-classed item to eager-load it's relationship but only when I want it to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文