2

I have two different data models that map to the same Car entity. I needed to create a second entity called ParkedCar, which is identical to Car (and therefore inherits from it) in order to stop nhibernate complaining that two mappings exists for the same entity.

public class Car
{
     protected Car()
     {
       IsParked = false;
     }

    public virtual int Id { get; set; }  
    public  bool IsParked { get; internal set; }
}

public class ParkedCar : Car
{
        public ParkedCar()
        {
            IsParked = true;
        }
       //no additional properties to car, merely exists to support mapping and signify the                           car is parked
}

The only issue is that when I come to retrieve a Car from the database using the Criteria API like so:

SessionProvider.OpenSession.Session.CreateCriteria<Car>()
                    .Add(Restrictions.Eq("Id", 123))
                    .List<Car>();

The query brings back Car Entities that are from the ParkedCar data model. Its as if nhibernate defaults to the specialised entity. And the mappings are defiantly looking in the right place:

<class name="Car" xmlns="urn:nhibernate-mapping-2.2" table="tblCar">

<class name="ParkedCar" xmlns="urn:nhibernate-mapping-2.2" table="tblParkedCar" >

How do I stop this?

Dan
  • 29,100
  • 43
  • 148
  • 207
  • It doesn't make sense to me why you want to separate the tables in the first place. Wouldn't you just use NHibernate to query for cars where `"IsParked = true"`? – Doug May 14 '10 at 15:22
  • It would take too long to explain why this needs to be the case. Basically in the application we are building certain entities need to be stored radically differently at certain times, but we want to shield the application from this. – Dan May 14 '10 at 16:06
  • But you aren't shielding your application from it at all. Your application needs to know when to query a Car, or when to query a ParkedCar, when both objects serve the same function. What I'm suggesting is a different architecture would alleviate the need to do this kind of thing at all. – Doug May 14 '10 at 17:40
  • You cannot make such statements without understanding the application or its domain model. What is your objective here? you are not aswering the question or helping me. Abstracting the difference between them is the correct thing to do in my case. The application knows the correct query to preform based on the IsParked property. – Dan May 16 '10 at 10:56

2 Answers2

3

I think you need to set the polymorphism property on the class mapping

<class "Car" polymorphism="explicit" ...
dotjoe
  • 26,242
  • 5
  • 63
  • 77
0

Since ParkedCar extends Car, a query for Car will return both Car and ParkedCar objects. You can restrict the type using HQL using the special class property, i.e. from Car c where c.class = Car. I don't think you can do this with the criteria API.

Alternatively you could filter the list after retrieving it if it's a reasonable size.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117