开发者

Abstract class and NHibernate

开发者 https://www.devze.com 2023-03-16 17:55 出处:网络
I have a class: class abstract Car { ... } class ConcreteCar { ... } class AnotherConcreteCar { ... } Yet it seems very confusing as to how to map these in NHibernate. I do need to be able to gat

I have a class:

class abstract Car {
    ...
}

class ConcreteCar {
    ...
}

class AnotherConcreteCar {
    ...
}

Yet it seems very confusing as to how to map these in NHibernate. I do need to be able to gather a:

List<Car> cars;
for (Car car in cars) {
    ...
}

I also would like to use a generator (what is the alternative?) for generating the next carID. There is join-subclass, which disallows that, and frankly, it is all confusing me. It seems I will have to go back to making Car an 开发者_开发百科interface, which isn't really what I want.

Also, no matter what method I use (say I just make Car a superclass and the other subclasses), if I query the database:

from item in session.Query<Car>()
select item;

will I have to typecast the objects into their subclass types to use the subclass properties and methods? Will that work? Does NHibernate actually figure out the subclass and create objects of the subclass, or does it just create objects of the superclass that cannot be converted into their subclasses?


I've implemented this kind of approach already a while ago but I remember it was working in my particular case.

I assume that Car is the base table with some shared columns, if so then you could map your entities in the 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 that you could execute your query:

from item in session.Query<Car>() select item;

And indeed you would have to typecast returned objects into their subclass types in order to access the subclass properties and methods but NHibernate would be intelligent enough to construct them properly for you.

NHibernate would achieve that using this kind of syntax (pseudo sql):

select

-- all columns from Car
-- all columns from ConcreteCar 
-- other columns from subclasses

case 
    when f1.SubId is not null then 1
    when f2.SubId is not null then 2
    when f3.SubId is not null then 3
    when f4.SubId is not null then 4
    when f5.SubId is not null then 5
    when f0.Id is not null then 0
end as type

from Car f0
left outer join ConcreteCar f1 on f0.Id = f1.SubId
-- other joins

So depending on the actual number of implemented subclasses this can have an impact on the performance.

I hope this answers your question.


it's quite possible.
see inheritance mapping.
You Can use session.Query<Car> and nHib will know to create objects of the appropriate subclass.
let me know if you have any more questions.


Yes it works!

This is my code:

public abstract class Vote:EntityBase

public class VoteComment:Vote,IVote

public class VotePhoto:Vote,IVote


internal List<Vote> Test()
{
   return session.Query<Vote>().ToList();
}

and this is the result:

Abstract class and NHibernate

0

精彩评论

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