-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem saving entities containing collections
PostPosted: Fri Sep 23, 2011 5:06 am 
Newbie

Joined: Fri Sep 23, 2011 4:47 am
Posts: 2
Hi guys, I need some help.

I'm just starting out with NHibernate and I'm using Fluent for mappings. Everything seemed to work fine until today.
Here is the story:

I have two tables in my db: Store and WorkDay
The first table contains info about the store, and the WorkDay table contains info about the days of week and start/end time when the store is open.
Store contains a Guid StoreID PK column that is referenced in the WorkDay table.
So I have a mapping file for Store where I have a HasMany association with the WorkDay table, and a corresponding POCO for Store.
Now, when I fill in all the necessary data and try to persist it to database, I get an exception telling me that the insert into table WorkDay failed because the StoreID had null value and the table constraint doesn't allow nulls for that column (which is, of course, expected behavior).

I understand the reason for this exception, but I don't know how to solve it.
The reason why the insert fails is because the StoreID gets generated upon insert, but the WorkDay collection gets saved first, in the time when the StoreID hasn't yet been generated!

So, how do I force NHibernate to generate this ID to pass it to dependent tables? Or is there another solution for this?

Thank you!


Top
 Profile  
 
 Post subject: Re: Problem saving entities containing collections
PostPosted: Fri Sep 23, 2011 5:22 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

You need to fill the the parent entity reference in child entity also (i.e variable referring the parent in child class) so that it takes the reference of parent entity primary key to set the FK column of the child

Cheers,
Harish


Top
 Profile  
 
 Post subject: Re: Problem saving entities containing collections
PostPosted: Fri Sep 23, 2011 5:35 am 
Newbie

Joined: Fri Sep 23, 2011 4:47 am
Posts: 2
Hi Harish, thanks for your reply!

Shouldn't this be done automatically?
This is what I have in my StoreMap:

Code:
HasMany(x => x.WorkDays)
      .Inverse().KeyColumn("StoreID").ForeignKeyCascadeOnDelete()
      .Cascade.All();


and this is what's in the WorkDayMap

Code:
References(x => x.Store).Column("StoreID");


Top
 Profile  
 
 Post subject: Re: Problem saving entities containing collections
PostPosted: Sun Sep 25, 2011 10:15 pm 
Newbie

Joined: Sun Sep 25, 2011 10:06 pm
Posts: 1
mindonwarp wrote:
Hi guys, I need some help.

I'm just starting out with NHibernate and I'm using Fluent for mappings. Everything seemed to work fine until today.
Here is the story:

I have two tables in my db: Store and WorkDay
The first table contains info about the store, and the WorkDay table contains info about the days of week and start/end time when the store is open.
Store contains a Guid StoreID PK column that is referenced in the WorkDay table.
So I have a mapping file for Store where I have a HasMany association with the WorkDay table, and a corresponding POCO for Store.
Now, when I fill in all the necessary data and try to persist it to database, I get an exception telling me that the insert into table WorkDay failed because the StoreID had null value and the table constraint doesn't allow nulls for that column (which is, of course, expected behavior).

I understand the reason for this exception, but I don't know how to solve it.
The reason why the insert fails is because the StoreID gets generated upon insert, but the WorkDay collection gets saved first, in the time when the StoreID hasn't yet been generated!

So, how do I force NHibernate to generate this ID to pass it to dependent tables? Or is there another solution for this?

Thank you!

Thanks you for the post.

_________________
Watch 50/50 Movie Online Free


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.