-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Problems with bag and not-null foreign keys
PostPosted: Tue Oct 18, 2005 5:55 am 
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


Top
  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 6:51 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
How do you expect NHibernate to save the LocationInformationId if there is not such field in the mapping? It doesn't work magically - it works only with fields that are explicitely defined. And it is not enough to specify this column in bag mapping.
So you must add <many-to-one> element as you already tried. And also have to specify inverse="true" on the bag mapping.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 8:54 am 
Hmm...
Thanks so far, but somehow it works magically only with the bag, if I tell the DB that the foreign Key Column in T_ClimateCharts can be null.
What means that there is the FK set as expected...

Maybe Nhibernate first inserts the ClimateChart with the FK set to null and then updates the row and sets the right FK?
(so I can see the right FK, but the column has to be nullable)
anyone heard of that?


Top
  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 9:51 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Well, i can be wrong, of course, and there is some magic in NH. :) If it was working with nulls allowed then reverse="true" should help you. It will define the right order of updates - climate first and then location.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 10:12 am 
Thanks for your help :lol:
no magic in NH? :cry:


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.