【问题标题】:Fluent nHibernate and JoinSubClasses流畅的 nHibernate 和 JoinSubClasses
【发布时间】:2009-03-12 04:54:03
【问题描述】:

我不确定这是我的 Fluent 配置有问题还是我的思维逻辑有问题。

基本上我有一个 Person 类,我从中继承了两个类,Author 和 Borrower(它是一个库系统)。我的映射是。

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id, "id");
        Map(x => x.Name, "name");

        // Subclasses
        AddPart(new AuthorMap());
        AddPart(new BorrowerMap());
    }
}

public class AuthorMap : JoinedSubClassPart<Author>
{
    public AuthorMap() : base("person_id")
    {
        Map(x => x.Country, "country");
        HasMany(x => x.Books).Cascade.All().WithKeyColumn("book_id"); 
    }
}

public class BorrowerMap : JoinedSubClassPart<Borrower>
{
    public BorrowerMap() : base("person_id")
    {
        Map(x => x.UserName, "user_name");
        HasMany(x => x.Schedule).Cascade.SaveUpdate().WithKeyColumn("borrower_id");
    }
}

现在,如果我运行 HQL“FROM Author a ORDER BY a.Name”,它将返回所有作者和借款人实体的列表,我显然只想要一个作者列表。请随时让我明白这一点。

【问题讨论】:

    标签: c# .net nhibernate fluent-nhibernate


    【解决方案1】:

    有几件事可以尝试:

    • 在每个子类映射中,将表名设置为WithTableName("Author")
    • person_id 是每个子类表上的键列吗?如果不是,请将base("person_id") 更改为base("key column name")

    例如,我刚刚使用以下映射测试了一个非常相似的查询:

    public class DigitalFreeSubscriptionMap : JoinedSubClassPart<DigitalFreeSubscription>
    {
        public DigitalFreeSubscriptionMap()
            : base("DigitalFreeSubscriptions")
        {
            WithTableName("DigitalFreeSubscriptions");
            ...
    

    public class FreeSubscriptionMap : JoinedSubClassPart<FreeSubscription>
    {
        public FreeSubscriptionMap()
            : base("FreeSubscriptions")
        {
            WithTableName("FreeSubscriptions");
            ...
    

    两者都是Subscription 的子类。在我测试的数据库中,有 1700 个 DigitalFreeSubscriptions,而有超过一百万个 FreeSubscripions(和其他类型的订阅)。 HQL 查询“FROM DigitalFreeSubscripion”返回 1700 个结果。

    每个请求,SubscriptionMap 的顶部:

    public class SubscriptionMap : AuditableClassMap<Subscription>
    {
        public SubscriptionMap()
        {
            WithTable("Subscriptions");
            Id(x => x.Id, "Subscriptions");
    
            AddPart(new FreeSubscriptionMap());
            AddPart(new DigitalFreeSubscriptionMap());
            // More sublass mappings and then the field mappings
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      相关资源
      最近更新 更多