-->
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: Sort By Value stored in a Map
PostPosted: Tue Mar 13, 2012 1:23 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
I would like to sort (orderBy) a collection of Content objects by the value of one of its associated elements stored in a map.

The Java for the value I want to order by is:

Code:
content.getMetaData().get("netPay").getValue();


Where each part is represented by the following annotation mapped classes:

    content: Content
    content.getMetaData(): FreeFormRecord extends FreeFromDatabaseObject implements Map
    content.getMetaData().get("netPay"): FreeFormField extends FreeFormDatabaseObject implements CharSequence
    content.getMetaData().get("netPay").getValue(): String

FreeFormDatabaseObject is the superclass of FreeFormRecord, FreeFormField and the previously unmentioned FreeFormList. The approach allows me to store arbitrary trees of strings stored in the leaves (FreeFormField).

FreeFormRecord actually implements the Map interface via composition - it has the field:

Code:
Map<String,FreeFormDatabaseObject> map = new HashMap<String,FreeFormDatabaseObject>();


In practice one could rewrite the Java as:

Code:
content.getMetaData().getMap().get("netPay").getValue();


How do I do rewrite this in HQL to go in the
Code:
session.createFilter(collection,hql)
method?

I've played with joins and the index() function but I can't get it to work.


Top
 Profile  
 
 Post subject: Re: Sort By Value stored in a Map
PostPosted: Tue Mar 13, 2012 1:31 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
I seem to get close with:

Code:
from Content c, FreeFormRecord ffr, FreeFormField fff join c.metaData.map cmm where index(cmm)='netPay'


But this obviously doesn't contain and 'orderBy' clause and returns a list of Object arrays containing 5 elements from 3 different classes.


Top
 Profile  
 
 Post subject: Re: Sort By Value stored in a Map
PostPosted: Tue Mar 13, 2012 2:52 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
This get me even closer:

Code:
select c from Content c join c.metaData.map cmm where index(cmm)='netPay' order by cmm.value


But unfortunately the list contains lots and lots of duplicates.
How do I get rid of them?


Top
 Profile  
 
 Post subject: Re: Sort By Value stored in a Map
PostPosted: Tue Mar 13, 2012 3:57 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
With some help from a friend this appears to work:

Code:
select distinct c,cmm.value from Content c join c.metaData.map cmm where index(cmm)='netPay' order by cmm.value


It returns a list of arrays each of which unsurprisingly contains a content object and the netPay value, but I can live with that.
I had to put the order by cmm.value in the distinct term otherwise it didn't like it.
The database isn't to know that there's only one netPay keyed value per metaData object - even if Java does know.


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.