Why can't NHibernate use Identity in a Table-Concrete Class mapping mapping subclass?

Several sources indicate that NHibernate cannot use the table identifier for a particular class and subclass of union. This is true, and what exactly is the reason for this?

+4
source share
2 answers

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

Consider the following example:

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

If you were to receive a vehicle whose specific type you do not know:

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

... and there were both a car and a truck with this Id (which is possible with an individual), which one will be returned to NHibernate? (there are more complicated cases, but this illustrates one possible problem)

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

+4
source

Why do you say that? I think I had several scenarios. Also this blog post claims the same thing.

Summing up the comments below: as in the example that Ayende has, if you request for all root types (so that "Select Party"), you can get duplicates for the ID. This fact, along with the UNION characteristic (returns only individual records), can give you unexpected results (missing records). This is why you cannot use an identity, but rather a hilo, which allows nhibernate to avoid duplicates.

+1
source

Source: https://habr.com/ru/post/1394357/


All Articles