-->
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.  [ 1 post ] 
Author Message
 Post subject: How to use substring sql function ?
PostPosted: Fri Feb 11, 2005 6:16 am 
Newbie

Joined: Thu Jul 22, 2004 10:39 am
Posts: 7
Location: France
Hi,

I'm trying to use substring in a hql query.
And I always get an error/exception.
Looking in this forum, I found 2 topics but it stille doesn't work.

I would like to execute the following query :
select distinct substring(cbb.nom, 1, 1) as letter from CyberBase as cbb order by cbb.nom asc

Could you help me to do it ?

All informations needed below

Hibernate version: 2

Mapping documents:

<class name="fr.cdc.cyberbase.bean.structure.CyberBase" table="cyberbase">

<cache usage="read-write"/>

<id name="id">
<generator class="native" />
</id>

<property name="nom" type="string" length="100" not-null="true" />
...
</class>

Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.currentSession();

String query = "select distinct substring(cbb.nom, 1, 1) as letter from CyberBase as cbb";
query += getCritereTri("cbb.nom", triCroissant);

List results = session.createQuery(query)
.setCacheable(true)
.list();


Full stack trace of any exception that occurs:
net.sf.hibernate.QueryException: undefined alias: substring [select distinct substring(cbb.nom, 1, 1) as letter from fr.cdc.cyberbase.bean.structure.CyberBase as cbb order by cbb.nom ASC]
at net.sf.hibernate.hql.PathExpressionParser.token(PathExpressionParser.java:103)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
at net.sf.hibernate.hql.SelectParser.token(SelectParser.java:154)
at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
at net.sf.hibernate.hql.ClauseParser.end(ClauseParser.java:114)
at net.sf.hibernate.hql.PreprocessingParser.end(PreprocessingParser.java:143)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:30)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:151)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:140)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:287)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1491)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1462)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:45)
at fr.cdc.cyberbase.bean.acces.CyberBaseDao.getCyberbaseLetters(CyberBaseDao.java:139)

Name and version of the database you are using:
Mysql 4.0.16




Thanx


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.