-->
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.  [ 4 posts ] 
Author Message
 Post subject: select a sub-select with the same table
PostPosted: Mon Jan 05, 2009 3:46 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
Hi,

I have the problem with HQL select command

getHibernateTemplate().find("select.....")

I have the main select with 2 tables tender and tender_bid (conected by id columns) and in the where condition is sub-select with the same table as in main select (tender_bid).

the problem is, main-select and sub-select has the same alias for tender_bid table.

HQL command:

Code:

return getHibernateTemplate().find("select new
com.freightservice.model.TenderCarrierBestBidInformationResultItem(" +
                "tender, tenderBid) " +
                "from Tender tender, TenderBid tenderBid " +
                "where tender = tenderBid.tender " +
                "and tenderBid.carrier = ? " +
                "and tenderBid.bidPrice = (" +
                "select min(b.bidPrice) " +
                "from tenderBid b " +
                "where b.carrier = tenderBid.carrier " +
                "and b.tender = tenderBid.tender)", carrier);


generated SQL command:

Code:
select
        tender0_.id as col_0_0_,
        tenderbid1_.id as col_1_0_
    from
        tender tender0_,
        tender_bid tenderbid1_
    where
        tender0_.id=tenderbid1_.fk_tender_id
        and tenderbid1_.fk_carrier_id=?
        and tenderbid1_.bid_price=(
            select
                min(tenderbid1_.bid_price)
            from
                tender_bid tenderbid1_
            where
                tenderbid1_.fk_carrier_id=tenderbid1_.fk_carrier_id
                and tenderbid1_.fk_tender_id=tenderbid1_.fk_tender_id
        )


the problem is, that the first tender_bid and the second (in sub-select) has the same alias tenderbid1_, and generated SQL command is WRONG.

correct SQL command:

Code:

select
        tender0_.id as col_0_0_,
        tenderbid1_.id as col_1_0_
    from
        tender tender0_,
        tender_bid tenderbid1_
    where
        tender0_.id=tenderbid1_.fk_tender_id
        and tenderbid1_.fk_carrier_id=?
        and tenderbid1_.bid_price=(
            select
                min(tenderbid2_.bid_price)
            from
                tender_bid tenderbid2_
            where
                tenderbid2_.fk_carrier_id=tenderbid1_.fk_carrier_id
                and tenderbid2_.fk_tender_id=tenderbid1_.fk_tender_id
        )


thanks

Ivan


Top
 Profile  
 
 Post subject: Not a Solution but a Different approach
PostPosted: Tue Jan 06, 2009 11:35 am 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi Ivan,
I'm not a pro in HQL/Hibernate but I would give a try like this:-
I don't know your data model but I have made assumptions Please read and use it cautiously.

Firstly, modify the query which would get all the minimum priced bids grouped by carrier.

Make sure this query does not return multiple records for the same carrier

select min(tenderbid1_.bid_price),tenderbid1_.id,tenderbid1_.fk_carrier_id,
tenderbid1_.fk_tender_id
from tender_bid tenderbid1_
group by tenderbid1_.fk_carrier_id,tenderbid1_.fk_tender_id,tenderbid1_.id)


And then try to achieve the same I don't know how to but the target SQL that you should be looking for needs to look like this.(This is how I would approach but I could be wrong!)

select
tender0_.id as col_0_0_,
tenderbid1_.id as col_1_0_
from
tender tender0_
left join (select min(tenderbid1_.bid_price),tenderbid1_.id,tenderbid1_.fk_carrier_id,
tenderbid1_.fk_tender_id
from tender_bid tenderbid1_
group by tenderbid1_.fk_carrier_id,tenderbid1_.fk_tender_id,tenderbid1_.id) tenderbid1_ on(tenderbid1_.fk_tender_id=tender0_.id)
where
tenderbid1_.fk_carrier_id=?

Hope this helps,
Srilatha.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 6:03 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
I think you can use DetachedCriteria


Code:
DetachedCriteria subquery = DetachedCriteria.forClass(Tender.class);
subquery.setProjection(Property.forName("bidPrice").min());

Criteria criteria = session.createCriteria(Tender.class, "t").add( Subqueries.eq(10, subquery) );
// add other criteria


Top
 Profile  
 
 Post subject: article
PostPosted: Wed Jan 14, 2009 12:05 pm 
Beginner
Beginner

Joined: Fri Oct 28, 2005 12:26 pm
Posts: 21
I faced this problem and solved it with detached criteria. Here's the page.
http://sites.google.com/site/developmen ... atement-wi
You would just replace propertyIn with propertyEq.


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