Saturday, November 15, 2008

NHibernate Loading of Super Class

Magic!!! Table with 10 records, but when everything is loaded, it became 30 records.

What happened here? I have a super class that is inherited by another 2 subclass and the mapping strategy use here is the table-per-hierarchy mapping. In a scenario where I have to load the superclass from the database to validate some information regardless of which subclass it is. The method always return 2 times more than the data found in the table.

Spending about an hour trying to debug and googling around to understand what when wrong here but the search was not very fruitful. I just cannot believe that NHibernate does not support such issue of loading of superclass only, without multiplying the records with each subclass that inherit the superclass.

No choice, I am back to the NHibernate document and check if I have missing out any critical config for the class elemenet on the hbm file. After reading thru the list in detail, I finally found a class element that describe (polymorphism (optional, defaults to implicit): Determines whether implicit or explicit query polymorphism is used.) something close to the problem I have. Thankfully after some testing to ensure it does not produce the problem I have, I concluded the solution to this problem is to define the superclass polymorphism element to explicit.

No comments: