Polymorphic queries
Polymorphic queries extend the polymorphic behavior of classes into the persistence layer. What I mean by this is – polymorphism gives us a base type variable that can hold a reference to a derived type and invokes methods from derives types at runtime, NHibernate is capable of loading correct instance of derived type when queried by base type. For instance, if you try to load a Benefit
entity by ID from database, but the benefit record in the database corresponds to SkillsEnhancementAllowance
, then NHibernate is capable of determining what the actual derived type the record needs to be mapped to (SkillsEnhancementAllowance
in this example) and instantiates the correct entity type. We have seen this in Chapter 3, Let's Tell NHibernate About Our Database during mapping of inheritance hierarchy of classes. Let me take a moment to show you a unit test from that chapter which showed polymorphic nature of the ISession.Get<T>
method:
public void MapsSkillsEnhancementAllowance...