Setting base class object to derived class object
I have four classes. Person, NaturalPerson (Inherits from Person),
GroupFamilyMember(Inherits from NaturalPerson), QuotationHolder(Inherits
from GroupFamilyMember).
They all share the same ID.
My problem is the following one:
There is a method that returns an existing NaturalPerson(stored in DB)
object based on a document number. Then, I have to create a
QuotationHolder, and I want that QuotationHolder object to contain the
retrieved NaturalPerson object.
The issue, is that I can´t cast the object like this (I know the reason):
QuotationHolder quotationHolder = (QuotationHolder) naturalPerson;
I tried creating a new QuotationHolder object and setting its values with
the naturalPerson´s object values using reflection.
But as I lose the reference to the retrieved object, when I want to save
in cascade, NHibernate gives me the following exception:
a different object with the same identifier value was already associated
with the session
I guess that its trying to save the object as a new one.
Just to consider:
The IDs are set using the HILO Algorithm. The mappings cannot be changed,
neither the classes.
No comments:
Post a Comment