-->
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.  [ 3 posts ] 
Author Message
 Post subject: Joining On Aggregate Fields
PostPosted: Wed Jul 05, 2006 9:17 am 
Newbie

Joined: Wed Jul 05, 2006 8:50 am
Posts: 2
Location: Austin, TX
I'm relatively new to Hibernate but have been working with SQL for over a dozen years. I'm using DB2 running on the iSeries and using Hibernate 3.1.2.

I have a query where I'm joining two different objects (tables) where the documentId of one is the most recent revision of a set of revisions for that document. I've come up with two alternative sql statements that will work equally as well and are quick to return results.

There are two tricky parts two either query option. The first is that I need to use the Criteria API because the query needs to support searching on a user-supplied combination of fields. The fields they choose for the search may vary. The second tricky part is that I need to be able to select just the max revision in the select clause instead of also returning a field I'm grouping by. In most cases, the grouped by column is required in the select but in this case on DB2 it is not.

The first version of the sql statement uses a subselect:
Code:
select
document_id,
compound_policy_term_data,
item_type,
revision_date as create_date,
description,
page_count
from ieaddta.waf_revised_documents
where document_id
in (select max(revision)
    from ieaddta.waf_document_revisions
    where compound_policy_term_data like '0001134440%'
    group by original);


The second version uses an inner join on another query leveraging an alias to complete the join. I'm not even sure if this is possible using Hibernate or how it would be done:
Code:
select
document_id,
compound_policy_term_data,
item_type,
revision_date as create_date,
description,
page_count
from ieaddta.waf_revised_documents wrd
inner join (select max(revision) as max_revision_id, original
    from ieaddta.waf_document_revisions
    where compound_policy_term_data like '0001134440%'
    group by original) max_document_revisions on wrd.document_id = max_document_revisions.max_revision_id;


My Hibernate code to create the criteria for the subselect looks like the following:
Code:
// search criteria is a proprietary object akin to the Criteria object in
// Hibernate
searchCriteria.setBusinessClass(RevisionMappingBean.class);
DetachedCriteria criteria = getCriteria(searchCriteria,
                excludedSearchProperties);
       
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.max("revisionDocumentId"), "maxRevisionDocumentId");
projectionList.add(Projections.groupProperty("originalDocumentId"), "originalDocumentId");
criteria.setProjection(projectionList);

// the statement is then executed at this point and yields both the max field and the originalDocumentId field


So given all of this, is there a way to get the subselect to just return the max field using Criteria objects to generate the SQL? If necessary, can I somehow influence the SQL and still use Hibernate to generate the SQL (at least the where clause)? Should I modify the code to wrap the subselect with another select that just returns the max and if so, then what's the cleanest way to do this through the Criteria API?

Any other ideas are welcome.

Thanks in advance,

Mark


Top
 Profile  
 
 Post subject: More information
PostPosted: Wed Jul 05, 2006 3:48 pm 
Newbie

Joined: Wed Jul 05, 2006 8:50 am
Posts: 2
Location: Austin, TX
Just a little more information on my issue in the hopes of soliciting a response from a Hibernate expert.

    HQL will allow me to just select the max revision (aggregrate) for the subquery but I lose the ability to get the dynamically built where clause.
    Using the Criteria API gives me the where clause but I cannot exclude the group by column from the subquery's select clause.


Here's an attempt to pull together the outer and subquery using the criteria api.

Code:
        searchCriteria.setBusinessClass(RevisionMappingBean.class);
        DetachedCriteria subQueryCriteria = getCriteria(searchCriteria,
                excludedSearchProperties);
       
        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.max("revisionDocumentId"), "maxRevisionDocumentId");
        projectionList.add(Projections.groupProperty("originalDocumentId"), "originalDocumentId");
        subQueryCriteria.setProjection(projectionList);

        String compoundPolicyTermValue = buildCompoundPolicyTermValue(searchCriteria);

        // if contains wildcard, then add like else add equals
        if (StringUtils.contains(compoundPolicyTermValue, WILDCARD)) {
            subQueryCriteria.add(Restrictions.like("compoundPolicyTermData",
                    compoundPolicyTermValue));
        } else {
            subQueryCriteria.add(Restrictions.eq("compoundPolicyTermData",
                    compoundPolicyTermValue));
        }
       
        DetachedCriteria outerCriteria = DetachedCriteria.forClass(RevisedWAFDocumentReferenceBean.class);
        outerCriteria.add(Property.forName("serialNumber").eq(subQueryCriteria));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 8:16 am 
Newbie

Joined: Mon Sep 17, 2007 11:42 pm
Posts: 16
Location: Auckland, New Zealand
Have a look guys if you're still interested. I've raised an issue and submitted a patch against trunk to address this.

Criteria api does not allow a groupProperty to _NOT_ be included in the selected columns
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2893

Basically it allows you to specify a groupProperty like this:
Code:
Projections.groupProperty("id.variableId",true)

Javadoc:
Quote:
@param excludeFromSelect
indicates not to include this parameter in the select clause,
and _only_ in the GROUP BY clause.


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