-->
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.  [ 7 posts ] 
Author Message
 Post subject: Insert query in hibernate
PostPosted: Mon Feb 16, 2009 3:54 pm 
Newbie

Joined: Fri Feb 13, 2009 6:40 pm
Posts: 6
Hi

I am trying to insert a row in a table. I am using session.save() method to save to the table.
I was wondering how to understand if the insertion is successful. In normal sql, insert returns a row count.
Is ther something like that in hibernate?

Thanks
Neeti


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 4:10 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate will throw an exception if the insert doesn't work. But note that depending on your configuration, id-generation strategy, which database you are using, etc. the insert may not happen immediately when you call save(). At the latest, it will happen when you commit your transaction, but depending on other things you are doing in the code, it may happen anytime in-between session.save() and commit(). There is some information about this in the Hibernate documentation http://www.hibernate.org/hib_docs/v3/re ... shing.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 4:21 pm 
Newbie

Joined: Fri Feb 13, 2009 6:40 pm
Posts: 6
Hi

I wanted to know the rows affected so that I can put a condition so that the user can know that the values are stored into the database without any problem.

Can I assume that if Hibernate does not give me an exception, the values are saved into the database and the row is affected? accordingly, I can code the application to show a success message to the front-end users, after session.close();

thanks
Neeti


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 4:23 pm 
Newbie

Joined: Fri Feb 13, 2009 6:40 pm
Posts: 6
Hi

I wanted to know the rows affected so that I can put a condition so that the user can know that the values are stored into the database without any problem.

Can I assume that if Hibernate does not give me an exception, the values are saved into the database and the row is affected? accordingly, I can code the application to show a success message to the front-end users, after
Code:

session.save();
tx.commit();
session.close();

thanks
Neeti


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 6:30 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Can I assume that if Hibernate does not give me an exception, the values are saved into the database and the row is affected?


If you don't get an exception everything should be ok.

Under what circumstances do you expect the row insert to fail? I mean, even if you were using plain JDBC you would normally get an exception if the insert couldn't be executed.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 7:21 pm 
Newbie

Joined: Fri Feb 13, 2009 6:40 pm
Posts: 6
I had an error in Insert statements and the application did not give an exception. I found that there was an error from the number of rows affected. That is why I asked if that is possible in Hibernate, just to have a check for testing.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 3:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I had an error in Insert statements and the application did not give an exception.


This seems very strange. Is it maybe an issue with the JDBC driver or database you are using? If the JDBC driver or database doesn't throw exceptions when things doesn't work, then Hibernate will not throw any exception either, and I guess things may get messed up if this happens.


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