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.