I have a donation table that has a CampaignID column that refers to a campaign table. I need to insert 0 in the CampaignID column instead of Null if the Campaign is not used for this Donation.
My mappings from the Donations table are as follows:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> <class name="Donation,Entities" lazy="true" table="Donations" dynamic-update="true" > <id name="DonationID" column="PledgeID" type="Int64"> <generator class="native" /> </id> <many-to-one name="FundraisingCampaign" class="Campaign, Entities" column="CampaignID" lazy="proxy" not-found="ignore" cascade="none" />
Before I save the database, I check if the campaign object for my donation object is zero. If so, I set it for the new campaign object and set CampaignID = 0 like this.
if (null == donation.FundraisingCampaign) { donation.FundraisingCampaign = new Campaign() {CampaignID = 0}; }
The problem is that I get ErrorMessage "the object refers to an unsaved transient instance - saves the temporary instance before flushing." when trying to save.
I don’t understand why this excites anything in my Campaign object other than CampaignID, because I have cascade = "none", it should not try to save anything in the campaign table.
I force the current system to set 0 instead of Null instead, so saving Null is not an option.
source share