开发者

Optional SubclassMap (left join) with NHibernate 3.0 GA & FNH

Disclaimer: I'm fairly new to NH & ORM in general.

Disclaimer: I'm working with a build of FNH from here in order to use with NH3.0GA.

The problem in a nutshell is that I would like to use FNH's SubclassMap as a way to map a LEFT JOIN, table-per-subclass scenario to my object hierarchy which is defined as:

public class MyBaseClass {
    public virtual int Id { get; set; }    
}

public class MySubClass : MyBaseClass {
    public virtual string SubClassVal { get; set; }
}

This is mapped via FNH as:

public class MyBaseClassMap : ClassMap<MyBaseClass> {
    public MyBaseClassMap() {
        Table("BaseClass");
        Id(x => x.Id, "Id").GeneratedBy.Assigned();
    }        
}

public class MySubClassMap : SubclassMap<MySubClass> {
    public MySubClassMap() {
        Table("SubClass");
        KeyColumn("Id");
        Map(x => x.SubClassVal);
    }
}

And I retrieve via:

public class Repository {
    ISession session; //assume properly initialized ISession
    public IList<T> GetAll<T>() where T: class {
        return session.CreateCriteria<T>().List<T>();
    }
}

And in my database, I've got 1 record in my BaseClass table, 0 records in SubClass.

Now, what I would like to do is pull the entity out as a MySubClass instance by doing something like this:

var rep = new Repository();
var subclasses = rep.GetAll<MySubClass>();

And of course, there are no instances in the returned collection as this is presumably performing an INNER JOIN underneath it all. This is where I'm stuck. I've managed to discover that specifying an 'optional' join is what I'm supposed to do. I've attempted to modify MySubClassMap to:

public class MySubClassMap : SubclassMap<MySubClass> {
    public MySu开发者_C百科bClassMap() {
        Join("SubClass", j => {
            j.KeyColumn("Id");
            j.Optional();
            j.Map(x => x.SubClassVal); // note that I've tried the map outside the Join() below, to no avail
        });
        //Map(x => x.SubClassVal);
    }
}

Compiling/running this presents me with the following (innermost) exception:

The element 'joined-subclass' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'join' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'property, many-to-one, one-to-one, component, dynamic-component, properties, any, map, set, list, bag, idbag, array, primitive-array, joined-subclass, loader, sql-insert, sql-update, sql-delete, resultset, query, sql-query' in namespace 'urn:nhibernate-mapping-2.2'.

I'll save posting the stack trace, but the jist of it is:

MyApp --> 
FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() --> 
NHibernate.Cfg.FluentConfiguration.BuildConfiguration()

I think that's all the relevant info. I suspect I may be bumping into a breaking change between this very new version of NH and version of FNH that isn't so new. But, as mentioned earlier, I am a rookie, and could well be doing something stupid. If this is the case, I'd very much appreciate somebody smacking me over the head with what probably should be obvious.

Thanks in advance.


Entities have one type, which doesn't change. If you have a record in your BaseClass table only, that entity is and will always be a MyBaseClass.

If entities can change their "type", you shouldn't use inheritance but composition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜