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.  [ 2 posts ] 
Author Message
 Post subject: Insert followed by update of foreign key?
PostPosted: Wed Oct 11, 2006 6:05 pm 
Newbie

Joined: Wed Oct 11, 2006 5:48 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 7:49 pm 
Newbie

Joined: Wed Oct 11, 2006 5:48 pm
Posts: 2
Found the problem - the inverse attribute between the classes was set to false. Don't have the problem when it's true.


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