-->
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.  [ 5 posts ] 
Author Message
 Post subject: equivalent to ResultSet.executeUpdate()
PostPosted: Mon Jan 12, 2009 3:23 am 
Newbie

Joined: Fri Jan 09, 2009 8:50 am
Posts: 5
Location: Bangalore
Hai

In Hibernate we are trying to insert data to database in DAO layer of our application my query is

in simple JDBC Program we can make sure that datas are inserted or not by using the ResultSet.executeUpdate() (which will return number of rows updated or deleted for the update and delete operation)

In hibernate program how can i make sure that data i am trying to insert to database is sucessfully inserted or not

In Short "How can i acheive the exectuteUpdate() method functionality of ResultSet Interface in Hibernate" to make sure that my operation is sucessfull


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 4:05 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
when u r using HQL, then use executeUpdate() on ur Query object.

if u r using Object based data insertion and deletion use save(),delete() and update() methods on session object.

for eg.,
if using HQL.
SessionFactory fact = new Configuration().configure().buildSessionFactory();
sess = fact.openSession();
Transaction tr = sess.beginTransaction();
sess.createQuery("delete from Hosts where hostId = " + new Integer(hid)).executeUpdate();

if Object based.,
Hosts h=new Hosts();
h.setName('Madan');
session.save(h); // insertion

// for deleting host object with id=41;
Hosts hd=(Hosts) session.find(Hosts.class,41);
session.delete(hd);

**) don't forget to commit ur transaction, then only session changes updated to DB.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject: equivalent to ResultSet.executeUpdate()
PostPosted: Mon Jan 12, 2009 4:30 am 
Newbie

Joined: Fri Jan 09, 2009 8:50 am
Posts: 5
Location: Bangalore
Hey Madan

Thanks for the Reply :) ur information are helpful for me and i got one more query if i am inserting the data means how can i acheive the executeUpdate() functionlaity


Thanks & Regards
S.Sathiya Moorthy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 5:14 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
if u r going to insert new Host information without HQL. then use following tech

Hosts h=new Hosts();
h.setName('Madan'); //
... set the values for new host object
session.save(h);

if u call save(), hibernate use insert query in background for inserting new row into table. if u wanna to explore that set show_sql property to true in hibernate.cfg.xml file.

Or if u going to save new objects throu HQL means.
sess.createQuery("insert into Hosts values('Prabhu',2)").executeUpdate();

when u r calling executeUpdate() from query object it returns number of rows affected by that query.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 5:25 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
hi friend if u feel my post helpful for ur solution, Don't forget to rate me.

_________________
If u feel it will help you, don't forget to rate me....


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