-->
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: Deleting data without specifying Id/key for object
PostPosted: Fri May 05, 2006 12:41 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
This is regarding deleting rows from the table w/o specifying primary key of table....

In SQL we can have a query of following type

Delete from table1 where nonprimarykeycolumn="criteria";

As far as I have seen in hibernate, session.delete() api needs an object whose key field/id is set in order to delete it.

In short can we say that hibernate doesnt provide such facilities and I have to go with a hard way of selecting a record using my criteria (in above case , nonprimarykeycolumn="criteria") , then do a session.delete() by passing individual record ........

Couldnt find an api which takes a collection of objects also which can be deleted directly......

Hope I have explained the problem properly.....any clue please

Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 1:13 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're using 3.1 or later, you can use DML to delete rows: that is, write an HQL query like this:
Code:
<query name="deleteMyClassByName">
  delete MyClass c where c.Name = :name"
</query>
There is no API for deleting a collection of objects. Either use DML like the above query, or else loop through the collection, deleting each object separately. This can be reasonably efficient if you're judicious with your use of session.flush().

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:36 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
As far as my u/s goes , we cant have any delete/update/insert in session.getnamedquery().......

hope you got my point......

if i am going wrong, could you please tell me how do we execute given named query using hibernate session apis in java class


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 6:13 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
What tenwit has told you is explained in hibernate reference:
Quote:
13.4. DML-style operations



You need:
Code:
session.getNamedQuery(queryName).executeUpdate();


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 6:46 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're not allowed to write queries to do deletes for you, then you'll have to use the Session.delete(object) method. Those are your options. I don't know what a "u/s" is, but you'll have to change it to allow deletes, or else take the performance hit and delete objects manually.

_________________
Code tags are your friend. Know them and use them.


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.