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]