-->
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.  [ 6 posts ] 
Author Message
 Post subject: newbie: Update Statement Query Exception
PostPosted: Tue Feb 14, 2006 5:40 pm 
Newbie

Joined: Wed Feb 08, 2006 11:41 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.2

Mapping documents:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.RateLockInfoWrapper" table="dbo.DLAS_Output">
<id name="id">
<column name="DLAS_RecID" not-null="true"/>
</id>
<property name="status">
<column name="DLAS_Record_Processed"/>
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():this.sessionFactory.getCurrentSession().createQuery(UPDATE_HQL).setParameter(0,String.valueOf(maxId)).executeUpdate();

Full stack trace of any exception that occurs:org.hibernate.QueryException: query must begin with SELECT or FROM: UPDATE [UPDATE test.RateLockInfoWrapper rateLockInfoWrapper set rateLockInfoWrapper.status='Y' where rateLockInfoWrapper.id <= ?]
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.(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:134)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:113)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1602)
at test.RateLockInfoDAO.updateSelectedRateLockInfos(RateLockInfoDAO.java:93)


Name and version of the database you are using:SQL Server

The generated SQL (show_sql=true):N/A

Debug level Hibernate log excerpt:org.hibernate.QueryException: query must begin with SELECT or FROM: UPDATE [UPDATE test.RateLockInfoWrapper rateLockInfoWrapper set rateLockInfoWrapper.status='Y' where rateLockInfoWrapper.id <= ?]


Hi,

I am doing something like this.sessionFactory.getCurrentSession().createQuery(UPDATE_HQL).setParameter(0,String.valueOf(maxId)).executeUpdate(); within my DAO to update rows of a table

The UPDATE_HQL string looks like this:
UPDATE test.RateLockInfoWrapper rateLockInfoWrapper set rateLockInfoWrapper.status='Y' where rateLockInfoWrapper.id <= ?

Hibernate is throwing a QueryException saying 'query must begin with SELECT or FROM: UPDATE'

Could you please let me know why this error is being thrown?
I also tried createSQLQuery instead with SQL statement instead of HQL. But, it seems I cannot call executeUpdate on a SQL statement.

How do I update values of certain columns of a table?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 5:46 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use session.connection().createStatement() or similar. Queries are for retrieving data from the database, not for putting data into it.


Top
 Profile  
 
 Post subject: newbie: Update Statement Query Exception
PostPosted: Tue Feb 14, 2006 6:09 pm 
Newbie

Joined: Wed Feb 08, 2006 11:41 am
Posts: 3
Thanks! That worked.

Is there any way to do this in HQL instead of SQL?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 6:50 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
HQL is also for queries, so no, you can't issue inserts or updates using it. You could create a new class with just the columns you want to update in it, and use the normal Session save methods.


Top
 Profile  
 
 Post subject: Problem with the documenation?
PostPosted: Sat Feb 25, 2006 4:30 pm 
Newbie

Joined: Thu Oct 09, 2003 11:07 pm
Posts: 7
I ran into a similar issue. The problem as I see it is the main hibernate docs seem to imply that you can issue an update statement using the createQuery syntax:
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#batch-direct

Code:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
        .setString( "newName", newName )
        .setString( "oldName", oldName )
        .executeUpdate();
tx.commit();
session.close();


Is this an error in the docs or am I missing something? Thanks for your help,

Jonathan.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 26, 2006 10:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You need to make sure you are using the new antlr-based query parser. The original poster was *not*; he was using the "classic" query parser which does not support update/delete/insert statements in HQL.


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