I've been trying to diagnose this for the better part of an hour, but no luck. I'm using a bag collection in a parent object, "Topic", along with a many-to-one reference from the child "Post".
When I add a post object to the Posts collection of Topic, I'm seeing two SQL transactions: one INSERT, followed by an UPDATE against the [topic_id], even though it was provided in the INSERT.
What is causing this behaviour? How can I disable it?
NHibernate Version 1.20 Beta1
DB: MS SQL 2005
Code:
<class name="TestApp.Topic, NHibernateExample" table="topic">
...
<bag name="[b]Posts[/b]" cascade="all" generic="true" access="field.camelcase-underscore">
<key column="topic_id"/>
<one-to-many class="TestApp.Post, NHibernateExample"/>
</bag>
...
</class>
Code:
<class name="TestApp.Post, NHibernateExample" table="post">
...
<many-to-one name="Topic" class="TestApp.Topic, NHibernateExample" column="[b]topic_id[/b]" />
...
</class>
Code:
ISession session;
try
{
session = nhFactory.OpenSession();
Topic topic = session.Load<Topic>(50);
Post post = new Post();
post.Topic = topic;
post.Subject = "This is a test";
post.Body = "blah blah blah";
topic.Posts.Add(post);
session.Flush();
}
finally
{
session.close();
}
But this SQL is being executed:
INSERT
Code:
exec sp_executesql N'INSERT INTO post (subject, post, [b]topic_id[/b]) VALUES (@p0, @p1, @p2);
select SCOPE_IDENTITY()',N'@p0 varchar(14),@p1 varchar(14),@p2 int',@p0='This is a test',@p1='blah blah blah',[b]@p2=50[/b]
UPDATE
Code:
exec sp_executesql N'UPDATE post SET topic_id = @p0 WHERE post_id = @p1',N'@p0 int,@p1 int',@p0=50,@p1=584
Thanks in advance,
- T