-->
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.  [ 4 posts ] 
Author Message
 Post subject: delete by (just) id?
PostPosted: Thu Mar 24, 2005 6:37 pm 
Newbie

Joined: Thu Mar 24, 2005 6:24 pm
Posts: 8
Hibernate version:2.1.6

I'm looking for a (good) way to delete an instance with just a Class and an Id. I wrote a delete method that does:

public void delete(Class type, Serializable id) {

....

// get the meta data for this class
ClassMetadata metadata = factory.getClassMetadata(type);

// new up an instance and set the id property
Object instance = type.newInstance();
metadata.setIdentifier(instance, id);

// delete the instance
session.delete(instance);
}

but I get an error -

"Could not delete instance of type com.xyz.common.dao.node.Node with identifier 10134: net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: com.xyz.common.dao.node.Node.name"

- because I have not set the name property - just the id.

If I load the full Object first, and then call session.delete(instance), it works. However, this seems inefficient to have to fetch the whole object just to delete it.

I could write SQL that would "delete from NODE where id = ... ", but that would bypass caching, if we want to turn it on later. (yes, that's one mitigation - but not an answer - to by efficiency concern)

Thanks,

-Dan


Top
 Profile  
 
 Post subject: delete("query")
PostPosted: Thu Mar 24, 2005 7:58 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
Have you tried the other Session.delete() methods? There are four, including:
Code:
public int delete(String query,
                  Object[] values,
                  Type[] types)
           throws HibernateException


You can probably create a query to do it. If you didn't have to be generic, you could simply call
delete("from MyClass where id=" + id);

Hopefully this helps a bit and isn't a rehash of stuff you've already tried.

scott


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 3:46 pm 
Newbie

Joined: Thu Mar 24, 2005 6:24 pm
Posts: 8
My concern in using the query style delete methods is that I would bypass the second level cache. I.e., objects that are deleted in this manner wouldn't get flushed from the cache. Is that unfounded?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 11:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
It is unfounded, because what Hibernate is really doing here is to first perform a select query to load those objects and delete them one-by-one.

If the entity is lazy/proxiable, the best solution is to just load it and then delete it (because in that case Session.load() will simply return you a proxy without actually loading the data from the DB).


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