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.  [ 3 posts ] 
Author Message
 Post subject: Hi,Problem In Deleting Row. Plz help
PostPosted: Mon Apr 23, 2007 7:09 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 12:04 pm
Posts: 25
Location: Chennai
Hi

Greetings!

Could anybody help me out for following query.

By running below code i m being able to delete the row where bookid=12.

long bookid = 12;
Session sess = HibernateFactory.currentSession();
Object book = sess.load(Book.class, bookid);
sess.delete(book);

now i want to delete the row based on any other column of the table which is not the primary key for that table.

Like could u get me the code for below query in the same way..

delete from tablename where bookname="abc";

Note:-Bookname is not the primary key.


Thanks.

With Regards
Saurav Jain

_________________
I Really want to share my knowledge and learn from you all people specaily about Hibernate.. Thanks Lot


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 7:30 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi intimate2talent ,

use Criteria. Try the following:
Code:
      Criteria crit = session.createCriteria(Book.class);
      List books = crit.add(Restrictions.eq("bookname", "abc")).list();
      
      Iterator it = books.iterator();
      while (it.hasNext()) {
         Book element = (Book) it.next();
         session.delete(element);
      }
      
      


You could also use HQL (closer to SQL).

chucky

Don't forget to rate if this helps
----------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 7:35 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
As far as I know, you cannot query the session without a primary key. So, here is one way.

for(Object book: session.createQuery("from Book where name = 'bookname'").list() )
session.delete(car) ;


Drawback:
select and delete statements.

Why don't you fire the delete query straight away ? ( as is )
session.createQuery("delete from Book where name = 'bookname'").executeUpdate() ;

----------------------------------------------------
Rate the reply if you find it helpful


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