-->
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: delete return value behavior
PostPosted: Mon Jul 11, 2005 3:34 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
The documentation for net.sf.hibernate.Session.delete() says that it will "Return the number of objects deleted." I am getting the following output:

count = 0
count = 1

after running the following:

s = sessionFactory.openSession();
//tx = s.beginTransaction();
System.out.println("count = " + count);
count = s.delete(hql);
System.out.println("count = " + count);
//tx.commit();

By commenting out the transaction lines, this code had no chance of deleting anything *from the database* . In what context do the dox use the word "delete"? Is this correct behavior?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 3:48 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
The same context as it would in straight JDBC.

Code:
//assume java.sql.Connection named conn
conn.setAutoCommit(false); //begin a transaction

PreparedStatement s = conn.prepareStatement("DELETE FROM SOME_TABLE");
int count = s.executeUpdate();

System.out.println(count);

conn.rollback();


The ouput count will be > 0 (assuming there are records in SOME_TABLE), even though the transaction is explicitly rolled back and no data is altered. It would be pretty useless to have an updated count returned from executeUpdate() if you couldn't use it until after the end of the transaction.

Hibernate would, by extension, work the same way, telling you (to be precise) how many rows will be deleted if the transaction is successful.


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.