-->
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: Batch delete with Hibernate 3 ?
PostPosted: Sat Mar 05, 2005 4:03 pm 
Newbie

Joined: Thu Feb 03, 2005 11:36 am
Posts: 11
Hello, I allways used the deprecated delete(String) method when I needed to delete several object at once...

Now, to conform to the version 3 API, how must I do to achieve my goal ?

I thought it could be :

session.createQuery("FROM AND WHERE clauses").list().clear();

but it does not work (nothing is deleted from the database).

Is it a problem with my mapping that prevent objects to be deleted or is it because this way is the wrong way ?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 05, 2005 10:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
HQL now supports UPDATE/DELETE statements. There are some limitations with this currently, especially with anything relating to <join/>, <joined-subclass/>, or <union-subclass/>. But outside of those current restrictions, you'd do:
Code:
session.createQuery("delete from MyClass where ...").setXXX(...).executeUpdate();


This is the EJB3 bulk operation stuff.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 06, 2005 6:38 am 
Newbie

Joined: Thu Feb 03, 2005 11:36 am
Posts: 11
Okay, thank you Steve I understand the logic now... I tried with a simple statement but there is a problem :

hsession.createQuery("DELETE FROM Message WHERE id=?").setString(0,messageId).executeUpate();

Produce a NPE and a cryptic error during parsing :
*** ERROR: <AST>:0:0: unexpected end of subtree

java.lang.NullPointerException
at org.hibernate.hql.antlr.HqlSqlBaseWalker.path(HqlSqlBaseWalker.java:2193)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2232)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:498)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.deleteStatement(HqlSqlBaseWalker.java:276)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:156)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:814)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:801)
at org.hibernate.impl.QueryImpl.executeUpate(QueryImpl.java:89)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 10:03 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Perhaps the error message can be improved.

try
Code:
hsession.createQuery("DELETE FROM Message WHERE id=?").setString(1,messageId).executeUpate();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 29, 2005 9:30 am 
Newbie

Joined: Mon Sep 15, 2003 10:07 am
Posts: 8
Location: Stuttgart, Germany
Remove the "FROM" from your query string. See http://forum.hibernate.org/viewtopic.php?t=940351


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.