views:

70

answers:

2

Hey,

I have a pretty simple NHibernate setup: an Item object which has a collection of ItemDetail objects. One-to-many relationship between them.

In my web application, I'm writing code similar to:

item.Details.Add(new ItemDetail { Item = item, Text = "blah" });
itemRepos.Save(item);

This all works well. However, now I'm writing a small console application to do some imports, reusing my NHibernate setup and mappings, but when executing the same code, my save attempt results in an exception being thrown warning me that ItemDetail.Item contains null or a transient item. I assume this means it is complaining that ItemDetail.Item refers to an item that has not been saved yet (the item most definitely is not null), but that sounds weird since it is exactly that item that I am saving.

Am I overlooking something trivial here?

A: 

The level of detail you provide is not really sufficient to know what is going on.

If I were to hazard a guess, it has to do with cascades and the bidirectional relation between Item and Detail. You probably specified a cascade rule in the OneToMany side of the relation, but none in the ManyToOne side. Try adding a cascade rule there.

A: 

Turns out the problem was that saving the Item failed due to a date formatting error (SQL error) but this was not caught. Then, when it went on to save the ItemDetails, it was complaining about Item not being saved.

It's kinda weird that it didn't raise an exception about the SQL error, no?

efdee