orm - Abstract class and NHibernate -
i have class:
class abstract car { ... } class concretecar { ... } class anotherconcretecar { ... }
yet seems confusing how map these in nhibernate. need able gather a:
list<car> cars; (car car in cars) { ... }
i use generator (what alternative?) generating next carid. there join-subclass, disallows that, , frankly, confusing me. seems have go making car interface, isn't want.
also, no matter method use (say make car superclass , other subclasses), if query database:
from item in session.query<car>() select item;
will have typecast objects subclass types use subclass properties , methods? work? nhibernate figure out subclass , create objects of subclass, or create objects of superclass cannot converted subclasses?
i've implemented kind of approach while ago remember working in particular case.
i assume car base table shared columns, if map entities in following way (i'm using fluent nhibernate
):
carmap:
public class carmap : classmap<car> { ... }
concretecarmap:
public class concretecarmap : subclassmap<concretecar> { public concretecarmap() { extends<carmap>(); // other properties // map(x => x.property).not.nullable(); } }
having execute query:
from item in session.query<car>() select item;
and indeed have typecast returned objects subclass types in order access subclass properties , methods nhibernate intelligent
enough construct them you.
nhibernate achieve using kind of syntax (pseudo sql):
select -- columns car -- columns concretecar -- other columns subclasses case when f1.subid not null 1 when f2.subid not null 2 when f3.subid not null 3 when f4.subid not null 4 when f5.subid not null 5 when f0.id not null 0 end type car f0 left outer join concretecar f1 on f0.id = f1.subid -- other joins
so depending on actual number of implemented subclasses can have impact on performance.
i hope answers question.
Comments
Post a Comment