-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate behavior in case of double-delete
PostPosted: Mon Dec 12, 2005 1:43 pm 
Newbie

Joined: Mon Dec 12, 2005 10:45 am
Posts: 12
What is the expected behavior of Hibernate in case of a double delete of an entity ?

I have some unit tests for detecting the raising of a StaleStateException in that situation:

// delete the user
userService.delete(user);

try {
// try to delete it twice
userService.delete(user);
fail("Deleting twice should raise an StaleStateException");
}
catch(StaleStateException e) {
// expected
}


The tests pass with MySQl and fail with Oracle 9i.
Shouldn't hibernate provides a consistant behavior whatever the underlying database ??

TIA.


Hibernate version:
3.1rc3

Mapping documents:
N/A

Code between sessionFactory.openSession() and session.close():
session.delete(id);
or
session.delete(entity);

Full stack trace of any exception that occurs:
N/A

Name and version of the database you are using:
MySQL 4.xx
Oracle 9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 4:42 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
double delete isn't error - you will do nothing second time, only

try from sql tool (sqlplus or mysql, psql tool )

delete from table
where id='nonexists'

you get only 0 rows deleted or like message and no error


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 4:53 am 
Newbie

Joined: Mon Dec 12, 2005 10:45 am
Posts: 12
snpesnpe wrote:
double delete isn't error - you will do nothing second time, only

try from sql tool (sqlplus or mysql, psql tool )

delete from table
where id='nonexists'

you get only 0 rows deleted or like message and no error


At the SQL level (using sqlplus or whatever) you are certainly right.

But at the Hibernate API level this is not always the case;
As I said before, the following code:

Code:
// first delete
HibernateUtil.getSessionFactory().getCurrentSession().delete(entity);
// double delete
HibernateUtil.getSessionFactory().getCurrentSession().delete(entity);

will pass without any error when the underlying database is Oracle9i but it will throw a StaleStateException with MySQL.

In this precise case, Hibernate exibit different behaviors depending on the underlying database. Isn't it a code smell ? I am just asking.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 5:29 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
it is problem with mysql, probably
postgresql don't return error for deleteing with no row - you can have return value that number of rows is 0

delete command can delete 0 or more rows and if command is correct then no error for any number of rows

try mysql from command (mysql client) - i haven't mysql and can't try


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.