-->
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.  [ 5 posts ] 
Author Message
 Post subject: help!! Wrong SQL being generated
PostPosted: Mon Sep 26, 2005 10:53 pm 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
Hibernate version:3.1beta1

Code between sessionFactory.openSession() and session.close():
Code:
      HibernateCallback callback = new HibernateCallback(){

         public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("select count(distinct specie) from Specie specie where specie.tradeNames like '%" + theTradeName + "%'");
            query.setCacheable(true);
            return query.uniqueResult();
         }
      };
      


Full stack trace of any exception that occurs:
Code:
22:44:59,718 ERROR run ServiceInvokationThread:64 - Error executing invokation!!! :(
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not execute query; uncategorized SQLException for SQL [select count(distinct specie0_.id) as col_0_0_ from Specie specie0_, Specie_TradeName tradenames1_, TradeName tradename2_ where specie0_.id=tradenames1_.Specie_id and tradenames1_.tradeNames_id=tradename2_.id and (. like '%%')]; SQL state [37000]; error code [-11]; Unexpected token: . in statement [select count(distinct specie0_.id) as col_0_0_ from Specie specie0_, Specie_TradeName tradenames1_, TradeName tradename2_ where specie0_.id=tradenames1_.Specie_id and tradenames1_.tradeNames_id=tradename2_.id and (. like '%%')]; nested exception is java.sql.SQLException: Unexpected token: . in statement [select count(distinct specie0_.id) as col_0_0_ from Specie specie0_, Specie_TradeName tradenames1_, TradeName tradename2_ where specie0_.id=tradenames1_.Specie_id and tradenames1_.tradeNames_id=tradename2_.id and (. like '%%')]
java.sql.SQLException: Unexpected token: . in statement [select count(distinct specie0_.id) as col_0_0_ from Specie specie0_, Specie_TradeName tradenames1_, TradeName tradename2_ where specie0_.id=tradenames1_.Specie_id and tradenames1_.tradeNames_id=tradename2_.id and (. like '%%')]
   at org.hsqldb.jdbc.Util.throwError(Unknown Source)
   at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
   at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:395)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:334)
   at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1503)
   at org.hibernate.loader.Loader.doQuery(Loader.java:638)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
   at org.hibernate.loader.Loader.doList(Loader.java:1959)
   at org.hibernate.loader.Loader.list(Loader.java:1928)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:405)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:293)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:862)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:630)
   at com.thewoodexplorer.dal.impl.SpecieDaoImpl$4.doInHibernate(SpecieDaoImpl.java:140)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:356)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:329)
   at com.thewoodexplorer.dal.impl.SpecieDaoImpl.getSpeciesByTradeNameCount(SpecieDaoImpl.java:144)
   at com.thewoodexplorer.service.impl.SpecieServiceImpl.getSpeciesByTradeNameCount(SpecieServiceImpl.java:583)
   at com.thewoodexplorer.service.impl.ServiceFacadeImpl$8.run(ServiceFacadeImpl.java:274)
   at com.thewoodexplorer.service.ServiceInvokationThread$1.run(ServiceInvokationThread.java:59)
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
   at java.lang.Thread.run(Unknown Source)


Name and version of the database you are using:
HSQL-1.8.0.1
The generated SQL (show_sql=true):
Code:
select count(distinct specie0_.id) as col_0_0_ from Specie specie0_, Specie_TradeName tradenames1_, TradeName tradename2_ where specie0_.id=tradenames1_.Specie_id and tradenames1_.tradeNames_id=tradename2_.id and (. like '%%')


Specie has a many-to-many relationship to TradeName. Because of this I need to use distinct; otherwise if 2 tradenames of a Specie match the like criteria they will be returned twice.

Any hint will be very appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 12:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So then does specie.tradeNames represent a collection of entities?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 12:24 am 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
yes it does.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 12:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You probably want something like:
select count(distinct specie)
from Specie specie inner join specie.tradeNames as tradeName
where tradeName.nameProperty like '%" + theTradeName + "%'

or even better:
Code:
String qry = "select count(distinct specie) " +
        "from Specie specie inner join specie.tradeNames as tradeName " +
        "where tradeName.nameProperty like :tradeNameParam";
Query query = session.createQuery( qry );
query.setString( "tradeNameParam", "%" + theTradeName + "%" );
query.setCacheable( true );
return query.uniqueResult();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 12:36 am 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
Hi Steve,
Thanks a lot. That is exactly what I was looking for.

cheers,

erick.


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