-->
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: Alias Problem
PostPosted: Mon Jul 03, 2006 9:19 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 that I am 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


Last edited by Paras on Thu Jul 06, 2006 10:15 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 8:57 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Without your mapping, it's difficult to show an example, but try looking at section 17.3.1 in the reference manual. It shows to to specify aliases in named queries.

HTH,


Top
 Profile  
 
 Post subject: plz solve my problem
PostPosted: Thu Jul 06, 2006 9:24 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
mojarrell wrote:
Without your mapping, it's difficult to show an example, but try looking at section 17.3.1 in the reference manual. It shows to to specify aliases in named queries.

HTH,


it has nothing to do with mappings the problem is only this that hibernate is just replacing my alias at one place but not at another place... named queries are totally different... i am making queries at run time


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:08 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Quote:
it has nothing to do with mappings the problem is only this that hibernate is just replacing my alias at one place but not at another place... named queries are totally different... i am making queries at run time


I apologize. I just suggested that if you could use named queries. (and nothing in your post leads one to believe you couldn't) then you could specify your aliases and avoid the problem you're having.


Top
 Profile  
 
 Post subject: Thanks But....
PostPosted: Thu Jul 06, 2006 10:14 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
Thanks 4 ur reply dear but plz solve my problem how can we stop hibernate from replacing my aliases.... or how will hibernate replace my alias at each and every point


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 12:31 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
One way to solve your problem would be to map the sub-select as an entity. Take this example of widgets which have an associated set of gadgets:

SQL:

Code:
-- drop the tables first
drop table gadgets;
drop table widgets;
drop table widget_gadget;

-- create the tables
create table widgets (
   widget_id identity
  ,widget_desc varchar(30)
  ,primary key(widget_id));

create table gadgets (
   gadget_id identity
  ,widget_id integer
  ,gadget_desc varchar(30)
  ,gadget_price double
  ,foreign key (widget_id) references widgets (widget_id)
  ,primary key(gadget_id));
 
insert into widgets values (null, 'widget1');
* wid_id ~
call identity();

insert into gadgets values (null, *{wid_id}, 'gadget1', 39.50);
insert into gadgets values (null, *{wid_id}, 'gadget2', 53.45);

insert into widgets values (null, 'widget2');
* wid_id ~
call identity();

insert into gadgets values (null, *{wid_id}, 'gadget3', 137.50);
insert into gadgets values (null, *{wid_id}, 'gadget4', 60);

commit;


Here is a sql query that retrieves data much as yours does:

Code:
select  widget_id
       ,(select sum(gadget_price)
           from gadgets
          where gadgets.widget_id = widgets.widget_id) as _sqry1
        ,widget_desc
  from widgets
order by _sqry1;


and here are the results of that query:

Code:
WIDGET_ID  _SQRY1  WIDGET_DESC
---------  ------  -----------
        0   92.95  widget1
        1   197.5  widget2


If your mapping docs look like this:

Code:
    <class name="Widget" table="WIDGETS">
        <id name="widgetId" column="WIDGET_ID">
            <generator class="native" />
        </id>
        <property name="widgetDesc" column="WIDGET_DESC" />
         <set name="gadgets" lazy="false" cascade="all">
            <key column="WIDGET_ID" />
            <one-to-many class="Gadget" />
        </set>
       
    </class>

    <class name="Gadget" table="GADGETS">
        <id name="gadgetId" column="GADGET_ID">
            <generator class="native" />
        </id>
        <property name="widgetId" column="WIDGET_ID" />
        <property name="gadgetDesc" column="GADGET_DESC" />
        <property name="gadgetPrice" column="GADGET_PRICE" />
    </class>


Then you can do code like this:

Code:
        lgr.info("Let's list Widgets");
        List widgets = widgetDao.getAllWidgets();
        it = widgets.iterator();
        while (it.hasNext()) {
            Widget widget = (Widget) it.next();
            lgr.info("Widget is: " + widget);
            Double sum = 0.0;
            Iterator gadIt = widget.getGadgets().iterator();
            while (gadIt.hasNext()) {
                Gadget gadget = (Gadget) gadIt.next();
                sum += gadget.getGadgetPrice();
                lgr.info("\tAssociated Gadget: " + gadget);
            }
            lgr.info("\tTotal price: " + sum);
        }


and you will get output like this:

Code:
INFO  Main - Widget is: Widget[0,widget1]
INFO  Main -    Associated Gadget: Gadget[1,gadget2]
INFO  Main -    Associated Gadget: Gadget[0,gadget1]
INFO  Main -    Total price: 92.95
INFO  Main - Widget is: Widget[1,widget2]
INFO  Main -    Associated Gadget: Gadget[3,gadget4]
INFO  Main -    Associated Gadget: Gadget[2,gadget3]
INFO  Main -    Total price: 197.5


If, however, you need to use SQLQueries, you can do this:

Code:
    public List testQuery() {
       
        Session session = getHibernateTemplate().getSessionFactory().openSession();
        SQLQuery query = session.createSQLQuery(
                "select  widget_id " +
                       ",(select sum(gadget_price) " +
                           "from gadgets " +
                          "where gadgets.widget_id = widgets.widget_id) as _sqry1 " +
                       ",widget_desc " +
                  "from widgets " +
                 "order by _sqry1");
        query.addScalar("widget_id", Hibernate.INTEGER);
        query.addScalar("_sqry1", Hibernate.DOUBLE);
        query.addScalar("widget_desc", Hibernate.STRING);
        return query.list();
    }


and then use the above method like this:

Code:
        lgr.info("Now test query: ");
        List objects = widgetDao.testQuery();
        it = objects.iterator();
        while (it.hasNext()) {
            Object [] elem = (Object []) it.next();
            lgr.info("widget_id: " + (Integer) elem[0]);
            lgr.info("\tSum is: " + (Double) elem[1]);
            lgr.info("\tDescription is: " + (String) elem[2]);
        }


and get output like this:

Code:
INFO  Main - Now test query:
INFO  Main - widget_id: 0
INFO  Main -    Sum is: 92.95
INFO  Main -    Description is: widget1
INFO  Main - widget_id: 1
INFO  Main -    Sum is: 197.5
INFO  Main -    Description is: widget2


Top
 Profile  
 
 Post subject: Hello
PostPosted: Fri Jul 07, 2006 12:34 am 
Newbie

Joined: Mon Jul 03, 2006 1:26 am
Posts: 9
Thanks 4 ur time given to me .... but this does not solved my problem, bec i m making queries at run time and i don't know what will user give, which columns will he select, which table name will he set as root table... we do not know anything.... everything is given at run time... once again thanks 4 ur lovely support but plz explore more to solve my problem


Top
 Profile  
 
 Post subject: Re: Hello
PostPosted: Wed Aug 30, 2006 12:44 pm 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
I know it's only a workaround, but if you write:
SELECT ....
ORDER BY 1

Hibernate should not substitute your alias ...
Bye


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.