-->
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.  [ 3 posts ] 
Author Message
 Post subject: hql - old and new parser
PostPosted: Sat Dec 25, 2004 7:23 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Hello,
I play with hql in hibernate3 beta1 - i try query from my post http://forum.hibernate.org/viewtopic.ph ... g+probably

It work with new hql parser.
This is query :

select bid.item.id,bid.item.name,min(bid.created)
from Bid bid"
group by bid.item.id,bid.item.name

New parser return this sql :

select bid0_.ITEM_ID as col_0_0_,
item1_.NAME as col_1_0_, min(bid0_.CREATED) as col_2_0_
from BID bid0_, ITEM item1_
where bid0_.ITEM_ID=item1_.ITEM_ID
group by bid0_.ITEM_ID , item1_.NAME

Old parser return sql :

select item1_.ITEM_ID as col_0_0_,
item1_.NAME as col_1_0_, min(bid0_.CREATED) as col_2_0_
from BID bid0_, ITEM item1_
where bid0_.ITEM_ID=item1_.ITEM_ID
group by bid0_.ITEM_ID , item1_.NAME

Sql from old parser doesn't work with oracle and postgresql (select clause have
item1_.ITEM_ID and group by clause have bid0_.ITEM_ID).
postgresql return error :
01:15:23,134 ERROR JDBCExceptionReporter: ERROR: column "item1_.item_id" must appear in the GROUP BY clause or be used in an aggregate function

This is test with caveatemptor (updated to hibernate3)
testHql1 method print old and new sql, but testHql2 is fine with new parser
when I set <property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
in hibernate.cfg.xml

testHql2 fail with default parser (without property hibernate.query.factory_class)

Code:
package org.hibernate.auction.test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.auction.exceptions.InfrastructureException;
import org.hibernate.auction.persistence.HibernateUtil;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.hql.QueryTranslator;
import org.hibernate.hql.QueryTranslatorFactory;
import org.hibernate.hql.ast.ASTQueryTranslatorFactory;
import org.hibernate.hql.classic.ClassicQueryTranslatorFactory;

/**
* @author snpe
*
*/
public class TestHql extends TestCaseWithData {

    public TestHql(String s) {
        super(s);
    }
   
    public void testHql1() throws Exception {
        initData();
        Map replacements = new HashMap();
        SessionFactoryImplementor factory = getSessionFactoryImplementor();
        boolean scalar=false;
        String queryString = "select bid.item.id,bid.item.name,min(bid.created) " +
        "\nfrom Bid bid" +
        "\ngroup by bid.item.id,bid.item.name" ;
    QueryTranslator newQueryTranslator;
    QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
    newQueryTranslator = ast.createQueryTranslator( queryString, Collections.EMPTY_MAP, factory );
    newQueryTranslator.compile( replacements, scalar );
    String newSql = newQueryTranslator.getSQLString();
    System.out.println("newSql=" + newSql);
   
    QueryTranslator oldQueryTranslator;
    QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
    oldQueryTranslator = classic.createQueryTranslator( queryString, Collections.EMPTY_MAP, factory );
    oldQueryTranslator.compile( replacements, scalar );
    String oldSql = oldQueryTranslator.getSQLString();
    System.out.println("oldSql  =" + oldSql);
    }
   
    public void testHql2 () throws Exception {
        initData();
        HibernateUtil.beginTransaction();
        Session session = HibernateUtil.getSession();
        List list = null;
      try {
         String queryString = "select bid.item.id,bid.item.name,min(bid.created) " +
         "\nfrom Bid bid" +
         "\ngroup by bid.item.id,bid.item.name";
         Query query = session.createQuery(queryString);
         list = query.list();
      }  catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      }
      HibernateUtil.commitTransaction();
      HibernateUtil.closeSession();
    }
   
    private SessionFactoryImplementor getSessionFactoryImplementor() {
        SessionFactoryImplementor factory = ( SessionFactoryImplementor ) getSessionFactory();
        if ( factory == null ) {
            throw new NullPointerException( "Unable to create factory!" );
        }
        return factory;
    }
   
    private SessionFactory getSessionFactory() {
        return HibernateUtil.getSessionFactory();
    }

}
[/code]


Top
 Profile  
 
 Post subject: the old parser has a bug then
PostPosted: Sat Dec 25, 2004 10:07 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
So, the new query translator generated SQL that worked, right?

Did the new query translator generate SQL that did what you expected though? Look like it.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 26, 2004 12:09 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
yes, new generator work fine.
old generator generate sql with different select and group by list in aggregate function
(only when exists identifier and yet another field from many to one class)
hsql do both queries, but oracle and postgresql not

regards


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