views:

128

answers:

1

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: 

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
Stuart Childs
Thanks. Can I see the map for your Subscription class?
Craig
Sure. Answer edited.
Stuart Childs