-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL update
PostPosted: Fri May 04, 2007 7:50 am 
Newbie

Joined: Tue Feb 28, 2006 5:03 pm
Posts: 15
Location: North Carolina
3.2.3

I want to run a mass update that will take data from one table and update rows from a different table. The SQL should look like this:
Code:
UPDATE Target
   SET targetField = src.sourceField
  FROM Source src, Target tgt
WHERE tgt.targetId = ?
   AND src.nameField = tgt.nameField


The objects Target and Source are not related in their respective .hbm.xml mappings, nor should they be. When I tried to create the HQL I did the following:
Code:
        try {
            String hqlUpdate = "UPDATE Target tgt SET tgt.targetField = src.sourceField FROM Source AS src WHERE src.nameField = tgt.nameField AND tgt.targetId = :id";
            Query q = getCurrentSession().createQuery(hqlUpdate);
            q.setLong("id", id);
            answer = q.executeUpdate();
            log.debug(METHOD, "" + answer + " Rows updated");
        } catch (Throwable e) {
            log.error(METHOD, "Hibernate problem", e);
        }

which results in the following exception:
Code:
2007-05-04 07:31:52,420 ERROR PARSER - line 1:90: unexpected token: FROM
2007-05-04 07:31:55,952 ERROR HqlStagingDao - matchStagingLoansToExisting::Hibernate problem
java.lang.IllegalArgumentException: node to traverse cannot be null!
   at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:31)
   at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:254)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
   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:1623)


Can this type of update be done through HQL or do I need to create SQL statement?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 8:11 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Here is an extract from HQL grammar.

updateStatement
: UPDATE^ (VERSIONED)?
optionalFromTokenFromClause
setClause
(whereClause)?
;


So, what you want might not be possible in HQL.

------------------------------------------------------------------
Rate the reply if you find it helpful


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