FIGDevelopers wrote:
Do I need to inherit patient class from Individual?
As I also have one more column in Patient and that's AddressUid. This refers to the Primary key AddressUid of the Address table.
In short I also need to
Patient objPatient = new Patient();
objPatient.Individual.FirstName = "xyz";
objPatient.Individual.LastName = "abc";
objPatient.externalID = 1234;
objPatient.Address.Address1 = "New Lane";
objPatient.Address.City = "Pune";
.
.
.
session.save(objPatient);
So then to make this work I will also need patient class to inherit from Address, which is not possible if it is already inherited from Individual.
You don't need to inherit from Address, you'll just need a one-to-one mapping with the Address table. Since the foreign key to Address is on the Patient table this should be pretty easy to do. Patient should inherit from Indivudual only.
FIGDevelopers wrote:
Also, how do I make NHibernate generate the primary for any table?
I tried
in Patient.hbm.xml
<id name="PatientUid" column="PatientUid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="foreign">
<param name="property">Individual</param>
</generator>
</id>
in Individual.hbm.xml
<id name="IndividualUid" column="IndividualUid" type="Guid">
<generator class="guid"/>
</id>
Look at the sample mapping that I provided above. As you'll see you have a class mapping for the base type Individual. The joined-subclass tag is for specific mappings related to Patient. It assumes that the Patient table has as a primary key the same key value as Individual and that a Patient can't be defined outside of the context of the Individual base table.
Patient's IndividualID has a foreign key dependency on Individual's IndividualID. When you save a new Patient record, NHibernate will first insert a record into Individual while generating the guid primary key. NHibernate will then use the same key to create a Patient record using Individual's primary key for the field specified in the key tag.
All fields that are common to Individual and Patient would have property tags in the Individual's class mapping (this assumes duplication of data between the two tables). Any fields specific to Patient yould have property tags in the joined-subclass section for the Patient class. In your example, this is where the one-to-one mapping to Address would be.
Does that make sense, or would you like me to make another mapping doc?
P.S. Don't forget to mark postings that help you solve your issue.