Hey!
I have a problem with a uni directional relation.
I have a Class ClimateChart and a Class LocationInformation.
One LocationInformation can have many ClimateCharts and
one ClimateChart belongs to one LocationInformation.
Quote:
ClimateChart n -- 1 LocationInformation
My Database tables look like this.
T_ClimateCharts:
----------------
ClimateChartID: Guid
Filename: string (not-null)
LocationInformationID: Guid (not-null)
T_LocationInformations:
-----------------------
LocationInformationID: Guid
Name: string (not-null)
OK, now I made 2 mapping files:
ClimateChart.cfg:
Code:
<class name="ClimateChart" table="T_ClimateCharts">
<id name="Id" access="property" column="ClimateChartID" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid.comb" />
</id>
<property access="property" not-null="true" name="Filename" type="String" column="Filename" />
</class>
LocationInformation.cfg:
Code:
<class name="LocationInformation" table="T_LocationInformations">
<id name="Id" access="property" column="LocationInformationID" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid.comb" />
</id>
<property access="property" not-null="true" name="Name" type="String" column="Name" />
<bag access="property" name="ClimateCharts" table="T_ClimateCharts" cascade="all">
<key column="LocationInformationID"></key>
<one-to-many class="ClimateChart" />
</bag>
</class>
I want it that way, becaus I want to have all ClimateCharts if I load the LocationInformation. I added one Property for Filename and one for Name and an IList for the Charts.
Now I would like to persist these object:
Code:
ClimateChart Chart = new ClimateChart();
Chart.Filename = "munic.jpg";
Info = new LocationInformation();
Info.Name = "Germany";
Info.ClimateChart.Add( Chart );
Info.Persist();
The method Persist() only calls
Code:
SaveOrUpdate(this);
Flush();
Now I get an error.
Can not insert Null in Column LocationInformationID in Table T_ClimateCharts
why that???? there should be a Guid for LocationInformation if I persist it. And the ClimateChart should refer to it.
So if I allow Null values in the ClimateChart Table for LocationInformationID it works, but nhibernate does not insert a null value.
The Foreign Key is set right. But I don't want to allow null values in the ClimateChart table.
I tried to use a bidirecional relation by adding this to my ClimateChart.cfg
<many-to-one access="property" name="LocationInformation" class="LocationInformation" not-null="true" column="LocationInformationID" />
Now befor persisting I additionally use
Chart.LocationInformation = Info;
and it works. But why! I don't want a bidirectional relation. Isn't it possible to do it unidirectional?
I think this is a very simple relation, often used. So what am I doing wrong?
I searched the Forum, but didn't find any help for me. So if there is, just post the URL.
Thanks for help
Michael