-->
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: Delete() function that takes an id instead of an object?
PostPosted: Fri Jul 15, 2005 4:28 am 
Newbie

Joined: Fri Jul 15, 2005 4:21 am
Posts: 14
I am curious about something in Hibernate. I tried to search the forum for an answer, but among the 6 700 hits I found, I didn't find one related to this (not that I checked them all, of course).

When you call Session.delete() you need to pass an object that you wish to delete, i.e., you first need to retrieve the object from the database in order to delete it. This seems rather odd to me. In my application we often only have the primary key, e.g., "personId", and I would like to use this to delete the object. Do I need to write HQL code to make this happen, or am I missing something? In iBatis you pass the primary key, not the object, to the delete() method, and that seems more intuitive to me.

Kristoffer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 7:33 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Write a simple template or use classic.Session's method:
Code:
public int delete(String query, Object value, Type type) throws HibernateException;


session.delete("from Foo where id = ?", new Integer(1), Hibernate.Integer);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 7:38 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Previous answer still involves object loading (under cover).

Use Query.executeUpdate() instead.

Query query = session.createQuery("from Foo where id = ?");
query.setInteger(0, 1);
int count = query.executeUpdate();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 8:27 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Code:
delete from Foo f where f.id = ?


I wouldn't think this would create a load, since it should result in something like:

Code:
DELETE FROM tbl_foo f WHERE f.ID_COLUMN = ?


Of course, if the ID is compound this would be written slightly differently.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 8:35 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
eagle79 wrote:
I wouldn't think this would create a load, since it should result in something like:


In H2 it definitely creates a load (see source).
But in H3 when using Query.executeUpdate() it doesn't.


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.