I have a table Donations which has a CampaignID column that relates to the Campaigns Table. I need to insert a 0 in the CampaignID column instead of Null if the Campaign is not used for this Donation.
My mappings from the Donations table looks like this:
<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 to the Database I check to see if the Campaign Entity on my Donation Entity is null. If so then I set it to a new Campaign Entity and set the CampaignID = 0 like this.
if (null == donation.FundraisingCampaign)
{
donation.FundraisingCampaign = new Campaign() {CampaignID = 0};
}
The problem is that I get an ErrorMessage "object references an unsaved transient instance - save the transient instance before flushing." when trying to Save.
I don't understand why it cares about anything on my Campaign object other than the CampaignID because I have cascade="none" it should not try to save anything to the Campaign table.
I am forced by the current system to set a 0 there instead of Null as well so saving Null is not an option.