-->
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.  [ 8 posts ] 
Author Message
 Post subject: BUG in Hibernate ...
PostPosted: Thu Jul 06, 2006 9:26 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
Hi I am facing an alias problem in Hibernate... I have a query which contains a sub query in the select list... I have defined an alias of the sub query as _sqry1 now I am putting an Order By clause in the query.... Order By is on the result of the sub query and I have given the alias of the sub query in the Order By clause

The Query is

SELECT (SELECT SUM(_0_ServiceCharge_0_service.chargeAmount) FROM Service AS _0_service , ServiceCharge AS _0_ServiceCharge_0_service WHERE _transportService_Challan = _0_service AND _0_service = _0_ServiceCharge_0_service.service) AS _sqry1 FROM Challan AS _Challan LEFT OUTER JOIN _Challan.transportService AS _transportService_Challan ORDER BY _sqry1

Now the problem is that Hibernate is replacing my aliases.... it replaced my alias of sub query from _sqry1 to col_0_0_ .... it replaced this alias in the select list but not in the Order By clause... i.e. it replaced the alias wher I defined it but it did not replaced the alias where I used it.... so the place where I am using the alias it is giving an error as undefined alias

when I manually replaced my alias in the Order By clause then my query run successfully

Hibernate version: 3.0.5

The generated SQL by Hibernate:

select (select SUM(servicecha3_.ChargeAmount) from Services service2_, ServiceCharges servicecha3_ where transports1_.TransportServiceId=service2_.ServiceId and service2_.ServiceId=servicecha3_.serviceGroupTablePKId) as col_0_0_ from Challans challan0_ left outer join TransportServices transports1_ on challan0_.TransportServiceId=transports1_.TransportServiceId left outer join Services transports1_1_ on transports1_.TransportServiceId=transports1_1_.ServiceId order by _sqry1

Name and version of the database you are using:

MS SQL Server 8.0

Full stack trace of any exception that occurs:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2148)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.daffodilwoods.framework.utils.HibernateUtil.main(HibernateUtil.java:296)
Caused by: java.sql.SQLException: [DataDirect][SQLServer JDBC Driver][SQLServer]Invalid column name '_sqry1'.
at com.ddtek.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.ddtek.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRPCNonCursorExecuteRequest.submitPrepare(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRPCExecuteRequest.doPrepExec(Unknown Source)
at com.ddtek.jdbc.sqlserver.tds.TDSRPCExecuteRequest.execute(Unknown Source)
at com.ddtek.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown Source)
at com.ddtek.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.ddtek.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.ddtek.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
... 8 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 11:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That looks like a report query, is it? Are you trying to build objects out of the result set, or just get scalars? If it's just a report, convert it to an sql-query and all will be well.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Doesn't Work
PostPosted: Fri Jul 07, 2006 1:11 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
tenwit wrote:
That looks like a report query, is it? Are you trying to build objects out of the result set, or just get scalars? If it's just a report, convert it to an sql-query and all will be well.


Hi thanks 4 ur support but it is not a report query... this query is generated at run time... actually we we have made a query parser in which user can give select attributes, group by attributes, where clause, order by attributes... so we are forming a HQL query which is converted to the underlying database, right now we are using MSSQL but it can mysql also, it can be lotus also... we do not know what the query will be.. so no question arises of making a sql query.....

U plz assist me that how can we stop hibernate from replacing our aliases... we want hibernate to use our alias and not to reproduce it's own

second even if hibernate is replacing our alias then it must replace all the occurences of our alias , not just at one place

third there is an operator "TOP" in MS sql, which is used when we apply ORDER BY in a sub query, without this, order by cannot be applied in a sub query. This operator gives the n top values of the sub query. is there any such operator in HQL also... plz remember i m making queries at run time


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 5:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want to generate the report on the fly, Criteria would be the first choice. Building your own query parser is just reinventing the wheel.

Even if it's generated on the fly, that doesn't mean that it's not a report query. The important characteristic of a report query is that all you're interested in is the returned result set, you don't care if it comes back as POJO entities or JDBC result sets. It may be that an sql-query is good enough in this case, and it will certainly be faster (as no entity objects have to be created).

Finally, wrt your main question: I don't think that you can do this in plain HQL. "select x as y" is not a valid HQL structure, as far as I'm aware. It's certainly not in the refdocs.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Criteria will not do
PostPosted: Mon Jul 10, 2006 2:52 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
Thanks ... but "criteria" will not solve our problem.... we have build a query parser which cab handle very complex queries, such queries can not be handled by using criteria.... not at all we have tried a lot but we were not able to shape our work in "criteria"


Now u plz help me and tell me how can we give an alias to a sub query in select list and we have to use that alias in the order by clause... this is my main problem... bec hibernate replaces my alias at one place and not at another

My query is

SELECT (SELECT SUM(_0_ServiceCharge_0_service.chargeAmount) FROM Service AS _0_service , ServiceCharge AS _0_ServiceCharge_0_service WHERE _transportService_Challan = _0_service AND _0_service = _0_ServiceCharge_0_service.service) AS _sqry1 FROM Challan AS _Challan LEFT OUTER JOIN _Challan.transportService AS _transportService_Challan ORDER BY _sqry1

May be I am wrong in expressing aliases, kindly assist.... now i look towards u with a grt hope that u will solve my problem


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 5:50 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
From my previous post:

tenwit wrote:
Finally, wrt your main question: I don't think that you can do this in plain HQL. "select x as y" is not a valid HQL structure, as far as I'm aware. It's certainly not in the refdocs.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Trapped
PostPosted: Wed Jul 12, 2006 12:45 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
Quote:
plz help me and tell me how can we give an alias to a sub query in select list and we have to use that alias in the order by clause... this is my main problem... bec hibernate replaces my alias at one place and not at another

My query is

SELECT (SELECT SUM(_0_ServiceCharge_0_service.chargeAmount) FROM Service AS _0_service , ServiceCharge AS _0_ServiceCharge_0_service WHERE _transportService_Challan = _0_service AND _0_service = _0_ServiceCharge_0_service.service) AS _sqry1 FROM Challan AS _Challan LEFT OUTER JOIN _Challan.transportService AS _transportService_Challan ORDER BY _sqry1

May be I am wrong in expressing aliases, kindly assist.... now i look towards u with a grt hope that u will solve my problem


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 1:20 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It

Cannot

Be

Done.


Sort it in memory, or use SQL. HQL will not do it.

_________________
Code tags are your friend. Know them and use them.


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