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: hibernate 3 and session.delete(query)
PostPosted: Wed Apr 13, 2005 5:42 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hi

How is it possible to delete rows from a table specified by a where condition in Hibernate 3? session.delete(query) is not supported anymore. Now I am deleting by iterating through rows selected with where condition but hibernate executes sql DELETE for each row.

Thansk in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 7:52 am 
Beginner
Beginner

Joined: Thu Mar 24, 2005 5:21 pm
Posts: 21
session.createQuery(...).executeUpate() [sic] *should* do the trick, but personally I've had no luck with it, at least using the old query parser.
YMMV with the new query parser; alternatively, you could get the connection from the session and do this with straight JDBC. (Bulk-delete is not really within the remit of ORM)
HTH
Satish


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 8:04 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hi

Thanks for reply. Unfortunately Hibernate 3 HQL doesn't support DELETE :-(.

Quote:
(Bulk-delete is not really within the remit of ORM)

I know that, but I can't help myself with this until my problem at http://forum.hibernate.org/viewtopic.php?p=2237945 is solved.

Thanks
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 9:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
Unfortunately Hibernate 3 HQL doesn't support DELETE :-(.

Untrue. It in fact does support delete statements.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 9:40 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
I apologize, you're right. I was using DELETE FROM table WHERE ..., but the correct way is DELETE table WHERE ... But I haven't found this in the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 10:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
No the correct way is "delete [from] ClassName [where ...]"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 10:40 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hi

I've added FROM and the exception has been thrown:

Code:
Transaction tx = session.beginTransaction();
  BusinessCase businessCase = (BusinessCase) session.get(BusinessCase.class, actionOnId);
  session.delete(businessCase); // (*)

// this throws exception (if I omit FROM keyword, it won't)
  session.createQuery("DELETE FROM Message WHERE MESSAGE_KEY_ID = :msgKey1 OR MESSAGE_KEY_ID = :msgKey2")
    .setParameter("msgKey1", businessCase.getTitleMessageKey())
    .setParameter("msgKey2", businessCase.getDescriptionMessageKey()).executeUpate();

  tx.commit();


Code:
ERROR parser - *** ERROR: <AST>:0:0: unexpected end of subtree
ERROR parser - *** ERROR: <AST>:0:0: unexpected end of subtree
ERROR MethodBindingImpl - Exception while invoking expression #{businessCaseList.delete}
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)eryImpl.java:89)
... (my code)


bug?

And one more issue:
As you can see, this excpetion was thrown in the transaction. Previous command (*) was to delete persistent object. Although the transaction wasn't commited, the object was removed from the cache (why?!), but not from database (OK).

Using Hibernate 3.0,Tomcat 5.0, Java 1.4.1


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.