-->
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 and formula sql query -subquery
PostPosted: Thu Aug 30, 2007 4:17 pm 
Beginner
Beginner

Joined: Tue Oct 10, 2006 3:23 am
Posts: 33
Hi,

I have a fairly complex sql query containing a subquery to be used in a formula:

Code:
@Formula("( select subqry.rank from ("+
      "SELECT   ph1.PH_ID,ph1.PH_PROTOCOL_ID,"+
      "DENSE_RANK () OVER (PARTITION BY ph1.ph_protocol_id
ORDER BY ph1.ph_history_date asc ) rank"+
            "  FROM pm_protocol_history ph1)subqry "+
              "where subqry.ph_id=PH_ID )")
            int versionNumber;


This does not work as the sql generated is:
Code:
( select subqry.rank from (SELECT   ph1.PH_ID,ph1.PH_PROTOCOL_ID,protocolhi0_.DENSE_RANK () protocolhi0_.OVER (protocolhi0_.PARTITION BY ph1.ph_protocol_id ORDER BY ph1.ph_history_date asc ) protocolhi0_.rank  FROM pm_protocol_history ph1)protocolhi0_.subqry where subqry.ph_id=protocolhi0_.PH_ID ) as formula0_1_

Ok my dialect (oracle9) doesn't know about rank, partition and over no big deal I added it.

However after this I still get an incorrect query were not know column are prepended by hibernate with the main entity name. Kind of expected but really not the behaviour I want.

I ended up doing this:

in my dialect:
Code:
for (int i=0;i<10;i++)
  registerFunction( "priv_ns_"+i, new StandardSQLFunction("priv_ns_"+i));


and in my entity mapping using 'priv_ns_[0-9]' as aliasing names:
Code:
@Formula("( select priv_ns_1.priv_ns_2 from ("+
         "SELECT   ph1.PH_ID,ph1.PH_PROTOCOL_ID,"+
         "DENSE_RANK () OVER (PARTITION BY ph1.ph_protocol_id ORDER BY ph1.ph_history_date asc ) priv_ns_2"+
            "  FROM pm_protocol_history ph1)priv_ns_1 "+
            "where priv_ns_1.ph_id=PH_ID )")
            int versionNumber;


Basically reserving names in the dialect. I know this is a HACK, however I was wondering if there were a better way of doing this, or if subquery aliasing will be supported anytime, well at least for formulas.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Alias and formula sql query -subquery
PostPosted: Wed Oct 31, 2007 5:45 pm 
Newbie

Joined: Wed Oct 31, 2007 5:36 pm
Posts: 13
I have the exact same need and have not been able to find a workaround. Hopefully someone can address this issue.

Thanks.



***Edit:
There is a workaround (using a named query or such to retrieve the calculated value) instead of loading upon entity retrieval. But I would like to save the trip to the DB if possible.


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.