I'm working on an ASP.NET application which uses Nhibernate and encountering very strange behaviour recently.
I have patient->patientAddress mapping in my Biz object. A patient can have more than 1 address but within the patient table we have a mailingAddress column which points to a row in Patint_Mailing table. my mapping file is开发者_StackOverflow社区 like this.
<many-to-one name="mailingAddress"
column="mailingAddress"
class="DataLoad.Biz.PatientAddress, DataLoadBiz" />
I'm generating a csv file with patient information and I would expect this file to include all the patients and their mailing address. This works absolutely fine sometimes but sometimes it's not.
For an example if Patient A has AddressA and AddressB. We select AddressB to be the mailing address within the application. If I compile this and run I get the csv file with Patient A with AddressB. Now if I go into SQL and delete the patient A and recreate the Patient A with the same setup I will get a csv file with Patient A with Address A even though in the database it says Patient A has the mailing address Address B
Any thought why this strage behaviour?
This is my Patient mapping file
<many-to-one name="practice"
column="practice"
class="DataLoad.Biz.Practice, DataLoadBiz" />
<many-to-one name="mailingAddress"
column="mailingAddress"
class="DataLoad.Biz.PatientAddress, DataLoadBiz" />
<bag name="addresses" lazy="false" >
<key column="patient"/>
<one-to-many class="DataLoad.Biz.PatientAddress, DataLoadBiz"/>
</bag>
<bag name="mailings" table="MAILING_PATIENT">
<key column="patient"></key>
<many-to-many column="mailing" class="DataLoad.Biz.Mailing, DataLoadBiz" />
</bag>
and this is the PatientAddress mapping file
<many-to-one name="patient"
column="patient"
class="DataLoad.Biz.Patient, DataLoadBiz" />
Once I update the patient details with the correct mailing address I can go to the database and see that it's properly updated. So there's no issue in there. but when nhibernate reading those values back from the DB it's seems to ignore the fact that I updated the mailingaddraeess
I have a web form with Original Address fields and Mailing Address fields. User can copy the Original Address across to Mailing Address and when they click the Next button I call the AddAddress() method in my patient class
public virtual void AddAddress(ref PatientAddress Address)
{
Address.patient = this;
this.addresses.Add(Address);
}
and in my Dao object I call the Save method
patientAddressDao.Save(updatedAddress);
then I updating the patient object's mailing address with the correct updated address
patient.mailingAddress = updatedAddress;
If you save Patient class ensure your Patient class have Cascade="All" or "all-delete-orphan" in address collection mapping.
When you say "We select AddressB to be the mailing address" are you setting a flag in Address class?
update
If you save Patient class use also inverse="true" in Patient class on address collection mapping if you have a bidirectional mapping.
update
the code you provide just add a new address but doesn't "select" it. You just have a list of two address refers to one patient. Where do you mark the address as mailaddress?
update
<many-to-one cascade="all" inverse="true" name="mailingAddress"
column="mailingAddress"
class="DataLoad.Biz.PatientAddress, DataLoadBiz" />
and
patientDao.SaveOrUpdate(patient);
精彩评论