-->
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.  [ 2 posts ] 
Author Message
 Post subject: Alias not working on projections
PostPosted: Fri Oct 03, 2008 3:15 am 
Beginner
Beginner

Joined: Mon Feb 04, 2008 7:36 pm
Posts: 31
Hibernate version:
2.1.0 Alpha 1

Name and version of the database you are using:
Sql Server 2005

I cant figure out how to use formulas in a criteria. This is a subset of what I am trying to do. I need to get a subquery as a property of a parent query. Then on the server I need to calculate a value and sort by that value. Finally, I will only return 20 rows.

Code:
    // get the cash balance
    DetachedCriteria dc = DetachedCriteria.For(typeof(Account), "a")
        .CreateAlias("Holdings", "holdings")
        .SetProjection(Projections.ProjectionList()
                .Add(Projections.Sum("holdings.Balance")))
         .Add(Expression.EqProperty("b.ID", "a.ID"));


    ICriteria cashPercent = session.CreateCriteria(typeof(Account), "b")
        .SetProjection(Projections.ProjectionList()
            .Add(Projections.Property("ID"))
            .Add(Projections.Property("Balance"))
            .Add(Projections.Alias(Projections.SubQuery(dc), "Cash"))
            .Add(Projections.SqlProjection("Cash / Balance As CashPercent",
                    new string[] { "CashPercent" },
                    new NHibernate.Type.IType[] { NHibernateUtil.Decimal })))
        .AddOrder(Order.Asc("CashPercent"));


    IList l = cashPercent.List();


The problem here is that the correlated subquery that I alias as CashPercent is not getting aliased. As it sits I get an exception:
Quote:
"could not resolve property: CashPercent of: XXX.Account"


If I remove the order statement I get a SQL error
Quote:
"Invalid column name 'Cash'."


Here is the SQL that is generated, you can see that y2_ is not being called "Cash"

Code:
SELECT this_.AccountID as y0_, this_.Balance as y1_,
(
   SELECT sum(holdings1_.mktvalue) as y0_
   FROM Account this_0_
      inner join holdings holdings1_ on this_0_.AccountID=holdings1_.AccountID
   WHERE this_.AccountID = this_0_.AccountID
) as y2_,
Cash / Balance As CashPercent
FROM Account this_



Is there some way that I need to set the aliases I use here. Both the aliases CashPercent and Cash seem to be getting ignored.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 04, 2008 4:15 pm 
Beginner
Beginner

Joined: Mon Feb 04, 2008 7:36 pm
Posts: 31
It appears that there are two problems here.

  1. NHibernate does not support NHibernate expressions in the select clause. It would support sql exprssions but unless you can get to the aliases generated by NHibernate, that wont work.
  2. Sub-queries in the from clause are not supported.


I could hack in a fix for number 1. Just post-process the sql and replace some special tag format with the generated aliasses. Bit messy but I think I could make it work.

The real problem is that sub-queries are not supported in the from clause. The only way to get around this is to hardcode sql. Obviously, this reduces database portablility, increases maintenance costs and creates more defect injection points (all bad).

Unfortunately, putting sub-queries in the from clause does not appear to be on the list anytime soon. (see NH Issue 1380) . So for now, I am going to have to bring all the rows back to the client and do the work there. I think this will end up preferable to hard coded sql.


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