-->
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.  [ 10 posts ] 
Author Message
 Post subject: Delete table contents
PostPosted: Fri Jul 07, 2006 6:12 pm 
Newbie

Joined: Fri Jul 07, 2006 5:45 pm
Posts: 1
I am having trouble in Hibernate 3 deleting all rows in the table. The HQL:

'DELETE FROM TABLE_NAME'

throws this exception:

org.hibernate.QueryException: query must begin with SELECT or FROM: DELETE [DELETE FROM Users]
at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:83)
at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:108)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:191)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:167)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)


Any insight would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 8:46 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
Quick note that might solve your problem:

in HQL you refer to your hibernate class name, not the TABLE_NAME. So if I have an HQL object as defined in the xml file to be named "RoadRunner" and the table is named "ROAD_RUNNER", you would:

DELETE FROM RoadRunnner

_________________
_________________
dan

If what I say is helpful, please rate the post as such by clicking 'Y'. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 8:09 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Yes, as dslevine said, you must use class names. Second, you must not call list() on the Query object that createQuery() returns. Call executeUpdate() do run hql delete statement (http://www.hibernate.org/hib_docs/v3/re ... tch-direct) :

Code:
Session session = HibernateUtil.currentSession();
session.beginTransaction();
   
Query q = session.createQuery("delete from Client");
q.executeUpdate();

session.getTransaction().commit();


How do you do your call ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 12:50 pm 
Newbie

Joined: Mon Jul 24, 2006 12:48 pm
Posts: 4
I have the same problem with hibarnate
Hibernate-Version: 3.1.3

I'm using the update method, and object names, already made few select queries, but the delete doesn't work


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 12:51 pm 
Newbie

Joined: Mon Jul 24, 2006 12:48 pm
Posts: 4
I have the same problem with hibarnate
Hibernate-Version: 3.1.3

I'm using the update method, and object names, already made few select queries, but the delete doesn't work


Top
 Profile  
 
 Post subject: cann't work
PostPosted: Mon Jul 24, 2006 10:15 pm 
Newbie

Joined: Mon Jul 24, 2006 9:45 pm
Posts: 3
Location: location
I have the same problem!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 2:54 am 
Newbie

Joined: Mon Jul 24, 2006 12:48 pm
Posts: 4
Here is more detailed description of the problem
Hibernate-Version: 3.1.3

I tried these methods how to delete something
1)
java code (from documentation):
Query query = session.createQuery( "delete from ErrorMessage e where e.portid = :portid ");
query.setString("portid", id);
query.setReadOnly(false);
query.executeUpdate()
result :
query must begin with SELECT or FROM: delete [delete from cz.ami.cac.db.ileas.ErrorMessage e where e.portid= :portid ]
2)
Session sess = HibernateFactory.currentSession();
session.delete("select e FROM cz.ami.db.ileas.ErrorMessage as e where portid = '" + id + "'");
result (strange): Unknown entity: java.lang.String

3)
This works, but it is not bad solution :
List<ErrorMessage> errorMessages = loadErrorMessages(id);
for (int i = 0; i < errorMessages.size();i++)
{
ErrorMessage errorMessage = errorMessages.get(i);
session.delete(errorMessage);
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 29, 2006 1:45 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
is portid a string value? is portid the name of the property in your object model? can you post your hibernate mapping file.

_________________
_________________
dan

If what I say is helpful, please rate the post as such by clicking 'Y'. I appreciate it.


Top
 Profile  
 
 Post subject: Re: Delete table contents
PostPosted: Thu Dec 27, 2012 5:20 pm 
Newbie

Joined: Thu Jan 26, 2012 11:56 am
Posts: 9
Same problem Here! i think that Hibernate just troll us...

i think they deprecate that functionallity or something


XD i found the answer here:
http://enlightensoft.wordpress.com/2011/07/01/how-to-resolve-org-hibernate-queryexception-for-delete-hql-query/

_________________
-------------------------------------------------------------
http://javahelp.redsaltillo.net


Top
 Profile  
 
 Post subject: Re: Delete table contents
PostPosted: Sat Dec 29, 2012 1:52 am 
Newbie

Joined: Thu Dec 20, 2012 11:11 pm
Posts: 6
Thank you, I think I still have many problems going to improve


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