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