-->
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.  [ 9 posts ] 
Author Message
 Post subject: UPDATE statement issued before DELETE issued
PostPosted: Wed Oct 25, 2006 2:20 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
When I call Session.Delete(o) on any of my objects, NH is issuing an update statement before it issues the delete.

I'm not sure why this is happening. I can't tell from the SQL because it is updating all the columns. Can anyone think of any reason why this behavior would make sense?

Thanks

P.S. Since I am passing objects through a web service, they are deserialized and then I open a new NH session, call Delete(), and close the session. I'm sure the problem must be related to this but I dont know what to do.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 1:46 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Can you show the SQL generated?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 9:25 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Sergey,

Thanks for the reply. The more I look at this, the more that I am sure it is a bug. Let me know if you agree and I need to file it somewhere.

I was wrong about it being related to transient objects. The extra UPDATE statement happens when :

- The object to be deleted has a bag of children objects, AND
- The object to be deleted has a timestamp/version column

To reproduce I created a small example, a 'Student' class with child 'Book' objects. Here is the code that I execute:

Code:
ISessionFactory factory = cfg.BuildSessionFactory();

Student s = new Student("Fred", "fred@test.com");
Book b = new Book("Math 101", s);
s.Books.Add(b);

using (ISession session = factory.OpenSession())
{
   // Create
   session.Save(s);
   session.Flush();

   // Unexpected UPDATE statement happens on this call before the delete statement
   session.Delete(s);
   session.Flush();
}


Here are the table definitions:

Code:
CREATE TABLE Student(
   [id] [int] IDENTITY(1,1) NOT NULL,
   [name] [varchar](50) NOT NULL,
   [email] [varchar](50) NOT NULL,
   [lastupdate] [datetime] NOT NULL)

CREATE TABLE Book(
   [id] [int] IDENTITY(1,1) NOT NULL,
   [studentid] [int] NOT NULL,
   [name] [varchar](50))


Here are the mapping files:

Code:
   <class name="Student" table="Student" lazy="false">

      <id name="Id" column="id" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>

      <version type="timestamp" name="LastUpdate" column="lastupdate" />

      <bag name="Books" lazy="false" inverse="true" cascade="all-delete-orphan">
         <key column="studentid" />
         <one-to-many class="Book"/>
      </bag>
      
      <property column="name" type="String" name="Name" not-null="true" />
      <property column="email" type="String" name="Email" not-null="true" />

   </class>

   <class name="Book" table="Book" lazy="false">

      <id name="Id" column="id" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>

      <many-to-one name="Student" class="Student" column="studentid" />

      <property column="name" type="String" name="Name" not-null="true" />

   </class>


And finally, here is the SQL (note the unexpected UPDATE statement):

Code:
NHibernate: INSERT INTO Student (lastupdate, name, email) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY(); @p0 = '10/26/2006 9:07:06 AM', @p1 = 'Fred', @p2 = 'fred@test.com'
NHibernate: INSERT INTO Book (studentid, name) VALUES (@p0, @p1); select SCOPE_IDENTITY(); @p0 = '8', @p1 = 'Math 101'
NHibernate: UPDATE Student SET lastupdate = @p0, name = @p1, email = @p2 WHERE id = @p3 AND lastupdate = @p4; @p0 = '10/26/2006 9:07:06 AM', @p1 = 'Fred', @p2 = 'fred@test.com', @p3 = '8', @p4 = '10/26/2006 9:07:06 AM'
NHibernate: DELETE FROM Book WHERE id = @p0; @p0 = '3'
NHibernate: DELETE FROM Student WHERE id = @p0 AND lastupdate = @p1; @p0 = '8', @p1 = '10/26/2006 9:07:06 AM'


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 9:27 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Here is a zip file with the sample project:

http://bitwidgets.com/bitwidgets/Window ... tion22.zip

I'm wondering if NH is trying to verify the timestamp of the parent before it starts deleting the children. But I dont think this is necessary because an exception would roll back the transaction anyway. Also, even if it were necessary, a small SELECT on a single column would be enough to check the timestamp?

Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 9:43 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Anybody got any ideas for me on this one?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 28, 2006 8:11 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
I think that what is going on here is:

* NHibernte detects that you deletes a student.
* NHibernate detects that it should delete the book.
* NHibernate detects that the books collection on the student change, and updates the student timestamp.
* Delete book
* Delete student


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 28, 2006 11:22 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Interesting... if this is true should it be considered a bug?


Top
 Profile  
 
 Post subject: should it be considered a bug?
PostPosted: Mon Nov 20, 2006 2:10 pm 
Newbie

Joined: Thu Sep 14, 2006 8:52 pm
Posts: 12
I have run into this issue as well. IMHO it is a critical bug. I recommend you log this as a bug.

I have not logged an JIRA issue before but if you recommend I will be happy to do it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 20, 2006 4:47 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I have not submitted as a bug


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