Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Basic relationship sorting question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Shannon Hicks  
View profile  
 More options Oct 19 2009, 7:25 pm
From: Shannon Hicks <iotas...@gmail.com>
Date: Mon, 19 Oct 2009 16:25:27 -0700 (PDT)
Local: Mon, Oct 19 2009 7:25 pm
Subject: Basic relationship sorting question
So, I have a relationship in one of my objects:

property name="myFavorites" fieldtype="many-to-many" CFC="Beer"
linktable="myBeers" FKColumn="userID" inversejoincolumn="beerID"
lazy="true" cascade="all" orderby="beerName";

This was fine and dandy for a first version, but now I want to be able
to get this list of beers according to my many-to-many relationship,
and I'd like to be able to change the sorting on the fly, and I would
also like to pass start and maxRows for pagination purposes.

Any ideas how I can do this? Is my only option generating a big ol'
dynamic HQL statement something like:

"FROM Beer JOIN User ON (Beer.beerID = myBeers.beerID AND User.userID
= myBeers.userID) ORDER BY beerStyle desc"

Shan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jon Messer  
View profile  
 More options Oct 20 2009, 10:44 am
From: Jon Messer <sylvan.mes...@gmail.com>
Date: Tue, 20 Oct 2009 07:44:18 -0700
Local: Tues, Oct 20 2009 10:44 am
Subject: Re: Basic relationship sorting question

You basically have 3 options:

1) write pure sql and populate objects by id (ick)
2) HQL like what you list (a lot of text manipulation meh)
3) use the Criteria API
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycrite...

Personally I like using the Criteria, especially for parametrized searching.
I've heard that Criteria queries are less performant then HQL, but unless I
ran into a specific problem I personally prefer the interface to writing
HQL. You will have to do some javacasting unfortunately, unless you use
cfgroovy or native java outside cf.

something like this (property names are clearly made up I don't know your
model):

restriction = createObject('java',"org.hibernate.criterion.Restrictions");
order = createObject('java',"org.hibernate.criterion.Order");
criteria = ormGetSession().createCriteria('Event');

criteria.add( restriction.like("userName", "%Cool%") )
           .addOrder( order.desc('beerStyle') )
           .createCriteria('myFavorites')
           .add( restriction.eq("beerScore", javacast('double',"50") )
           .setMaxResults(50)
           .setFirstResult(1) );

arrayOfBeer = criteria.list();

The pagination might not work depending on your DB though.

 HTH

Jon


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Silverberg  
View profile  
 More options Oct 20 2009, 10:55 am
From: Bob Silverberg <bob.silverb...@gmail.com>
Date: Tue, 20 Oct 2009 10:55:14 -0400
Local: Tues, Oct 20 2009 10:55 am
Subject: Re: Basic relationship sorting question

Cool stuff, Jon.  I have another couple of suggestions as well:

1. If using HQL, you can avoid the manual join by using "member of", like
so:

"select b from Beer b, User u where u.id = :UserId and b member of
u.myFavorites order by b.beerStyle desc"

2. You could use a collection filter.  These are not supported via CF
integration, but you can create them yourself using the Hibernate session
(similar to what Jon did with the Criteria object).  Here's an example that
I think will give you what you want:

    User = entityLoadByPK("User",1);
    filter = ormGetSession().createFilter(User.getmyFavorites(),"order by
this.beerStyle desc").setFirstResult(0).setMaxResults(20);
    beersArray = filter.list();

One issue with this is that you need to have a persistent User object first
(hence the first line call to entityLoadByPK). The criteria approach might
work better if you just want to run a query, without having a User object
already, and it also allows you to isolate your order by criteria a bit
more.  These filters are very cool, though, as they basically allow you to
run queries against your collection properties, so, for example, you could
paginate a collection, or change sort sequence, or subselect.

I'm going to write a post about them with a bit more detail, when I can find
the time. ;-)

Oh, one more thing. You misspelled favourite. ;-)

Cheers,
Bob

On Tue, Oct 20, 2009 at 10:44 AM, Jon Messer <sylvan.mes...@gmail.com>wrote:

--
Bob Silverberg
www.silverwareconsulting.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Raymond Camden  
View profile  
 More options Oct 20 2009, 11:25 am
From: Raymond Camden <rcam...@gmail.com>
Date: Tue, 20 Oct 2009 10:25:11 -0500
Local: Tues, Oct 20 2009 11:25 am
Subject: Re: Basic relationship sorting question
Thanks for sharing that "member of" tip there. This is the second HQL
"oddity" I've seen (I'm only calling it odd as I've never seen
anything like it in SQL). I'd love to see a blog post on HQL
"extensions" to normal SQL like this.

On Tue, Oct 20, 2009 at 9:55 AM, Bob Silverberg


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Silverberg  
View profile  
 More options Oct 20 2009, 11:45 am
From: Bob Silverberg <bob.silverb...@gmail.com>
Date: Tue, 20 Oct 2009 11:45:59 -0400
Local: Tues, Oct 20 2009 11:45 am
Subject: Re: Basic relationship sorting question

Thanks Ray.  I'll add it to the ever-growing list of things that I plan to
blog about ;-)

--
Bob Silverberg
www.silverwareconsulting.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian O'Sullivan  
View profile  
 More options Jun 16 2011, 5:30 am
From: Ian O'Sullivan <ian.osulli...@gmail.com>
Date: Thu, 16 Jun 2011 02:30:57 -0700 (PDT)
Local: Thurs, Jun 16 2011 5:30 am
Subject: Re: Basic relationship sorting question

Hi Guys

I know this thread is old but I just found the need to do this also. The
'member of' syntax is really cool and I was totally unaware of it. I'm using
it now but has there been a blog post about it since?

Ta

Ian.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »