Announcement Announcement Module
Collapse
No announcement yet.
Exception with Spring 1.2.5 and Hibernate 3 during update.. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception with Spring 1.2.5 and Hibernate 3 during update..

    I have the following method in my code.

    Code:
        public void updateConversionFlags (String servername) {
        	String query = "update AsyncData r set r.conversionRequired='TRUE' where r.status='PROCESSING' or r.broadcastStatus='PROCESSING' or r.broadcastStatus='RETRY' ";
    
        	if (servername!=null && !servername.equals("All")) {
        		query = query+" and r.server='"+servername+"'";
        	}
        	
        	//calls the Hibernate query updating object (see ConversionFlagUpdater)
        	HibernateTemplate ht = getHibernateTemplate();
        	ht.execute(new ConversionFlagUpdater(query));
        }
    The above will create a HQL query such as...

    update AsyncData r set r.conversionRequired='TRUE' where r.status='PROCESSING' or r.broadcastStatus='PROCESSING' or r.broadcastStatus='RETRY' and r.server='FOOBAR'

    I have the following HibernateCallback implementing class..

    Code:
    class ConversionFlagUpdater implements HibernateCallback{
    	String query;
    	public ConversionFlagUpdater (String query) {
    		this.query = query;
    	} 
    	public Object doInHibernate(Session session) {
    		Query hqlQuery = session.createQuery(this.query);
    		hqlQuery.executeUpdate();
    		return null;
    	}
    }

    However I'm getting the following exception (just a snippet). Why doesn't this code allow me to do an update?

    Code:
    Error in update: org.springframework.orm.hibernate3.HibernateQueryException: query must begin with SELECT or FROM: 
    update [update AsyncData r set r.conversionRequired='TRUE' where r.status='PROCESSING' or r.broadcastStatus='PROCESSING' or r.broadcastStatus='RETRY' and r.server='FOOBAR']; 
    nested exception is org.hibernate.QueryException: query must begin with SELECT or FROM: update [update AsyncData r set r.conversionRequired='TRUE' where r.status='PROCESSING' or r.broadcastStatus='PROCESSING' or r.broadcastStatus='RETRY' and r.server='FOOBAR'] org.hibernate.QueryException: query must begin with SELECT or FROM: update [update AsyncData r set r.conversionRequired='TRUE' where r.status='PROCESSING' or r.broadcastStatus='PROCESSING' or r.broadcastStatus='RETRY' and r.server='FOOBAR'] 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:176) at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:152) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427) at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884) at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:865) at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89) at

  • #2
    It doesn't seem like a Spring's problem.
    I'm not sure if it could be a cause, but in HQL you can't use table aliases for update/delete.

    Comment


    • #3
      Originally posted by dvoytenko
      It doesn't seem like a Spring's problem.
      I'm not sure if it could be a cause, but in HQL you can't use table aliases for update/delete.
      I know.. you use classes.. and that *is* the class. And you're right, it's not a spring problem as I've since found it's the choice of parser used in the Hibernate configuration for Spring...

      It appears setting this in the hibernate properties should fix the problem. I'm testing it now..

      Code:
      <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>

      Comment


      • #4
        I had the same problem only that i had explicitly specified org.hibernate.hql.classic.ClassicQueryTranslatorFa ctory for <prop key="hibernate.query.factory_class">

        I did not specify org.hibernate.hql.ast.ASTQueryTranslatorFactory, only removed the property from the xml file and it works.

        Great post although, as i googled specifically for "query must begin with SELECT or FROM" and found nothing, only when looking for something else i stumbled upon this. 10x

        Comment

        Working...
        X