简体   繁体   中英

NHibernate derived base class

we have a NHibernate Entity called Order. The Order has a List of Positions and some other stuff.

I now implemented a new Entity called OrderMin, which is now a base class of the Order. The same I did for the OrderPosition, which has now a base class called OrderPositionMin

When I try to load an OrderMin-Collection, I get a strange behavior: In the collection, there are now OrderMin and Order objects, this is my Code:

    var mins = Session.QueryOver<OrderMin>()
                    .Where(x => RestrictionExtensions.IsIn(x.Id,
                        list))
                    .List();

When I take a look in the collection, the containing Order objects has now 2 Lists Positions Lists. One is from the type OrderPosition and one from OrderPositionMin.

I tried to use the override keyword in the Order Object, but this is not possible due all Properties must be virtual.

Does anyone have an idea what's going wrong here?

Thanks in advance, Dennis

This is a default behavior in NHibernate. You need to declare a base class from which your Order and OrderMin derive.

public abstract class OrderBase
{
    // The properties that are needed by all derived types
}

public class OrderMin : OrderBase {}

public class Order : OrderBase {}

After that, your query result should look like you expect it. If you query a base class, NHibernate will materialize all entities of the base class and all entities of the derived types.

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