-->
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: Sort by associated map values
PostPosted: Mon Jan 21, 2008 1:16 pm 
Newbie

Joined: Fri Jan 20, 2006 1:54 pm
Posts: 1
I have a class, called "Folder", which has a mapped "map":

Code:
  <map cascade="all-delete-orphan" lazy="false" table="Folder_Attribute" name="attributes" outer-join="true" batch-size="30">
      <key column="folderid"/>
      <index type="string" column="`KEY`"/>
      <element type="string" column="value" length="4000"/>
    </map>


I want to paginate folders, and allow sorting of the folder based on the map--for example, if one of the map keys is "yearPublished", then I'd like to sort the folders based on the yearPublished value, so that folders with a map that have "1990" for the yearPublished value come before folders with a map that have "2000" for the yearPublished value.

I've been able to actually accomplish most of what I'm trying to do with the following query:

Code:
select folder from Folder folder inner join folder.attributes attrs
where index(attrs) = '" + mapAttribute + "'
order by attrs desc


I create a Query object with that string, and then call setFirstResult and setMaxResults on the query object, and everything works. Note that by using the above string, I'm not actually fetching all attributes at that time, as that would mess up the setFirstResult and setMaxResults.

The problem is that if a particular folder does not have the map key in question, it won't show up in the list of results, but I'd like to see all folders in the results, even if they don't have the map key in question.

I can't do a union all of the first set of folders with something like "select folder from Folders folder where folder not in (theFirstQuery)", because hql doesn't support union all. I suppose I could do it in two separate queries--see how many results come back from the first query, and if it is less than the page size, run the "not in" query to get some more results. Alternatively, I could drop down to native sql, where union all is supported. But, before I pursue one of those options, I'm wondering if there is just a simple way I'm overlooking to get the folders that don't have the map key in question included in the original results.

Any help would be appreciated.


Top
 Profile  
 
 Post subject: Re: Sort by associated map values
PostPosted: Mon Jan 21, 2008 7:18 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
First off, thanks for the question. For some unknown reason I kept thinking about this for hours and I actually could sort out a couple of things on the way. Anyhow, it's hard to come to an HQL that is able to do this due to a couple of HQL limitations, for some of which I have no explanation and I do believe it is just a matter of implementation. However, you can still use a native query for this. I did a little example that resembles much of your situation: a configuration class that has a map of properties. Assume the mapping for the properties is:

Code:
       <map name="properties" cascade="all" table="Property" inverse="false" fetch="join" outer-join="true">
            <key column="ConfigurationID"/>
            <index type="string" column="pKey"/>
            <element type="string" column="pvalue"/>
        </map>



then the following code would do what you want:

Code:
        Query query = session.createSQLQuery("select c.id, c.name from Configuration c left outer join Property p on p.configurationid = c.id and p.pkey = 'p1' order by p.pkey desc, p.pvalue asc").
                setResultTransformer(new AliasToBeanResultTransformer(test.model.data.Configuration.class));


'p1' in this example is a property name which some configurations have and some might not have. The returned list of configurations are in an ascending order of p1 values for the configurations that have it and the result of configurations following that last configuration with a p1 property.

You can still do two queries and merge the results manually if a native query is not a solution for you. Meanwhile we can still see if there is any other way to solve this.


Farzad-


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.