-->
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.  [ 7 posts ] 
Author Message
 Post subject: Deleting without loading first?
PostPosted: Sun Jul 08, 2007 5:20 am 
Beginner
Beginner

Joined: Thu May 10, 2007 4:20 am
Posts: 27
Hi,

I searched the documentation and the forum,
but still couldn't find a clear answer -

Can an object be deleted from the database without loading it first?
The id of the object is known in advance,
and it's a heavy and complicated object, so loading it just for deletion is inefficient.

Thanks,
Vitaliy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 3:43 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
I suppose Session.delete(...) or Query.executeUpdate() can do the trick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 2:34 am 
Beginner
Beginner

Joined: Thu May 10, 2007 4:20 am
Posts: 27
Hi,

Thanks for replying,
but if I understand the documentation correctly,
both functions you've mentioned work on persistent or transient objects only -
that is objects that had been loaded before at some stage as on object in the first case, or as part of a collection in the second case.

This is not what I'm looking for - I need to delete an object from the database that had never been loaded before, and loading which just for deletion is highly inefficient.

Thanks,
Vitaliy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 6:05 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
what about

Code:
Query query = session.createQuery("delete org.foo.MyObj where id=?");
// set id here
query.executeUpdate()


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 8:14 am 
Beginner
Beginner

Joined: Thu May 10, 2007 4:20 am
Posts: 27
This has no effect on the data.
I think you must path a from clause in the query.

Here's the code I've tried, like you suggested -

-------------------------------------------------------------------------------------
Session session;
Transaction transaction;
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
session.createQuery("delete com.package.ClassName where id=7"); transaction.commit();
------------------------------------------------------------------------------------

and here's the regular HibernateUtil:
-------------------------------------------------------------------------------------
public class HibernateUtil
{

private static final SessionFactory sessionFactory;

static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml

sessionFactory = new Configuration().configure().buildSessionFactory();

}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}

}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}

-------------------------------------------------------------------------------------


Thanks,
Vitaliy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 4:39 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
http://www.hibernate.org/hib_docs/v3/reference/en/html/batch.html#batch-direct states FROM clause is optional, after all it is HQL not SQL query.

the query above should work. could you please enable show_sql, and check return value from executeUpdate()?


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Wed Jul 11, 2007 8:57 am 
Beginner
Beginner

Joined: Thu May 10, 2007 4:20 am
Posts: 27
I tried that, but it didn't seem to modify the data.
I'll give it another shot, and update this post when I get the results.

Thanks,
Vitaliy


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