简体   繁体   中英

Fluent nHibernate and JoinSubClasses

I am not sure if this a problem with my Fluent configuration or some logic in my thinking.

Basically I have a Person class from which I have two inherited classes, Author and Borrower (it's a library system). The mapping I have is.

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");
    }
}

Now if I run the HQL "FROM Author a ORDER BY a.Name" it will return a list of all Author and Borrower entities where I obviously just want a list of authors. Please feel free to set me straight on this.

A few things to try:

  • In each of the subclass maps, set the table name with WithTableName("Author")
  • Is person_id the key column on each subclass table? If not, change base("person_id") to base("key column name")

For example, I just tested a very similar query with the following mappings:

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

and

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

Both are subclasses of Subscription . In the database I tested on there are 1700 DigitalFreeSubscriptions while there are over a million FreeSubscripions (and other kinds of subscriptions). The HQL query " FROM DigitalFreeSubscripion " returned 1700 results.

Per request, the top of the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM