Updating one-to-many relationship causes Insert error

121 views
Skip to first unread message

geekedout

unread,
May 9, 2009, 4:14:48 PM5/9/09
to Fluent NHibernate
I posted this on StackOverflow as well, hoping to find a quick
solution:

I'm trying to build a modified role system that has the following
class/relationship structure:

Project + ProjectRole + Employee = ProjectEmployee

I have a one-to-many mapping from Project to ProjectEmployee and a
reference mapping from ProjectEmployee to Project.

I can add new ProjectEmployee's just fine (which means my Create
activities are fine as well), but I want to constrain the relationship
to have only one entry per Employee. This affects the logic of my
Update activities.

In hacking this together, I am attempting to empty the
Project.ProjectEmployees (by setting the value to a new List<T>) and
then adding a new instance of ProjectEmployee with the appropriate
role, etc.

In doing this, I get an error that is related to setting
Project.ProjectEmployees to an empty List:

"Cannot insert the value NULL into column 'ProjectId', table
'AppDB.dbo.ProjectEmployees'; column does not allow nulls. UPDATE
fails. The statement has been terminated."

If I remove the line that news up the List<T>, the subsequent records
get added, but then I'm staring at too many records per employee.

Here's my Project -> ProjectEmployees mapping:

mapping.HasMany(x => x.ProjectEmployees) .Cascade.All() .WithKeyColumn
("ProjectId");

And the mapping in ProjectEmployee -> Project:

mapping.References(x => x.Project, "ProjectId");

Should be simple right?

For what it's worth, I also added a method to my Project class called
AddProjectEmployee that searches for the Employee in question, removes
his ProjectEmployee if he exists, then adds the new ProjectEmployee. I
get the same error when I call this.

Any help would be appreciated.

Nick

Hudson Akridge

unread,
May 9, 2009, 7:20:54 PM5/9/09
to fluent-n...@googlegroups.com
You don't want to clear out a persisted collection by newing the collection, you want to actually call .Clear() on the collection itself. If you've got your cascade set up correctly (all/all-delete-orphan) then NHibernate will remember that when it retrieved the collection back the first time, it had one element, then it had 0, then it had a different element in it. It will then issue a delete command on the first element, and an insert command on the new one.

geekedout

unread,
May 11, 2009, 3:41:23 PM5/11/09
to Fluent NHibernate
Thanks Hudson, that was perfect, and I feel sheepish for not having
picked up on that to begin with.

Must RTFM... ;)

On May 9, 5:20 pm, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> You don't want to clear out a persisted collection by newing the collection,
> you want to actually call .Clear() on the collection itself. If you've got
> your cascade set up correctly (all/all-delete-orphan) then NHibernate will
> remember that when it retrieved the collection back the first time, it had
> one element, then it had 0, then it had a different element in it. It will
> then issue a delete command on the first element, and an insert command on
> the new one.
>

James Gregory

unread,
May 11, 2009, 6:01:02 PM5/11/09
to fluent-n...@googlegroups.com
Easy mistake to make, I think we've all done it :)
Reply all
Reply to author
Forward
0 new messages