سؤال

Several sources state that NHibernate can not use identity with table per concrete class and union-subclasses. Is this true, and what is the exact reason behind this ?

هل كانت مفيدة؟

المحلول

It's simple. The POID must be unique across all instances of a root entity type.

Consider the following example:

abstract class Vehicle { ... }
class Car : Vehicle { ... }
class Truck : Vehicle { ... }

If you were to retrieve a Vehicle whose concrete type you don't know:

var carOrTruck = session.Get<Vehicle>(vehicleId);

...and there were both a Car and a Truck with that Id (which is possible with identity), which one would NHibernate return? (there are more complex cases, but this illustrates one possible issue)

Therefore, for table-per-concrete-class (a pretty bad strategy if you ask me), NHibernate needs a generator that guarantees uniqueness across subclasses.

نصائح أخرى

Why do you say so? I think I had several scenarios like that. Also this blog entry states the same.

To sum up comments below: as in the example that Ayende has, if you query for all root types (so "Select Party") you can get duplicates for ID. That fact along with UNION characteristic (returns only distinct records) could give you unexpected results (missing records). That's why you cannot use identity but rather hilo which allows nhibernate to avoid duplicates.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top