【问题标题】:nHibernate Per table class map, formula reference parent preopertynHibernate Per table class map,公式引用父属性
【发布时间】:2013-09-05 15:05:23
【问题描述】:

TL;DR

以这里的“子类”为例

https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping

我想在ChildMap 中有一个公式,它使用ParentName 属性

详细信息

我有一个继承层次结构(Product -> StockProductKitProductListingProductVariationParentProduct),其中 Inventory_Product 表包含所有公共字段,Inventory_StockProduct/Inventory_KitProduct 等子类的特定字段。我相信这被称为子类表映射。

在我的基类Product 上,我有一个名为ProductType 的属性。

public abstract class Product
{
    public virtual Guid ProductID { get; set; }

    public abstract ProductType ProductType { get; }

    public virtual int AvailableQuantity { get; set; }
}

public class StockProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.Stock; }
    }
}

public class KitProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.Kit; }
    }
}

public class ListingProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.Listing; }
    }
}

public class VariationParentProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.VariationParent; }
    }
}

public enum ProductType
{
    Stock = 0,
    Kit = 1,
    Listing = 2,
    VariationParent = 3
}

还有映射

public class ProductMap : ClassMap<Product>
{
   public ProductMap()
    {
        Table("Inventory_Products");

        Id(x => x.ProductID).GeneratedBy.Guid();
        Map(x => x.Sku);

        Map(x => x.ProductType).CustomType<ProductType>().Access.ReadOnly();
    }
}

public class StockProductMap : SubclassMap<StockProduct>
{
    public StockProductMap()
    {
        Table("Inventory_StockProducts");

        KeyColumn("ProductID");

        Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 0 THEN (1) ELSE NULL END)");
    }
}

public class KitProductMap : SubclassMap<KitProduct>
{
    public KitProductMap ()
    {
        Table("Inventory_KitProducts");

        KeyColumn("ProductID");

        Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 0 THEN (2) ELSE NULL END)");
    }
}

我需要以某种方式在StockProduct/KitProduct 上定义的公式中访问它以执行不同的 SQL

我试过StockProduct

Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 0 THEN (1) ELSE NULL END)") 

和套件产品

Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 1 THEN (2) ELSE NULL END)") 

但是 nhibernate 使用 [KitProduct|StockProduct] 的表别名而不是基本产品前缀 ProductType。所以它引用的列不存在

父对象的公式中是否有与this. 等效的值?

【问题讨论】:

  • ProductType 可以是 1 还是 0?
  • 将您的整个地图和课程置于问题之中。
  • 请发布示例代码,以便我们为您提供帮助。另外,以后请务必将您的代码标记为代码,以使其更具可读性!
  • 抱歉,第一次发帖到 SO,不确定规则 :) 现在更新帖子
  • 根据我的经验,最好将表名/列包含在 [] 中,以防止 NHibernate 在原始 sql 中修改它们,例如(CASE [ProductType] WHEN 1 THEN (1) ELSE NULL END)

标签: c# sql nhibernate


【解决方案1】:

我认为同时拥有一个类层次结构和一个属性来告诉你它是什么具体类并不是一个好的设计。您当然不需要此列来实现 Table per Class

你应该在基类中使用它:

UseUnionSubclassForInheritanceMapping();

这在子类映射中:

Abstract();

但是,我认为您正在尝试实现 Table per Type

有关三种不同映射策略的详细说明,请参阅这篇文章:http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

【讨论】:

  • 我理解这些差异。也许我应该解释我要解决的根本问题。我不需要 Type 列,我只是想用它来解决我的终极问题。我希望为每个子类的 AvailableQuantity 列运行不同的 SQL。默认情况下,nHibernate 为每个子类的每个子类类型运行 SQL,然后只使用相关的 SQL 来填充对象。这个 SQL 可能非常昂贵,因此当我想做的所有事情时运行 ListingProduct、VariationParentProduct 和 KitProduct 的 SQL 效率很低对我的 StockProduct 有价值
  • 我忘了补充,unionsubclass 的东西似乎效率很低。如果我想对父类上的字段进行查询,那么它需要进行联合。
  • @AndrewMurphy 嗯,根据我的经验,这并不是低效的,我在一些巨大的桌子上使用它,只要两张桌子上都有钥匙,它运行得非常快。我不明白你想为两个子类运行什么不同的 SQL,因为你的例子都是关于固定值的,你可以在子类中为 AvailableQuantity 提供不同的映射,那为什么还不够好?
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多