簡體   English   中英

流利的nHibernate和JoinSubClasses

[英]Fluent nHibernate and JoinSubClasses

我不確定這是否是我的Fluent配置問題或我的思維邏輯。

基本上我有從我有兩個繼承的類,作者和借款人(這是一個圖書館系統)一個Person類。 我的映射是。

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“從a.Name編寫作者的FROM作者”,它將返回所有Author和Borrower實體的列表,而我顯然只需要一個作者列表。 請隨時讓我直接講。

可以嘗試的幾件事:

  • 在每個子類映射中,使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM