Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA: order by length?
JPA: order by length? [message #385414] Fri, 30 January 2009 11:05 Go to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
Is there a way (aside from native queries) to do an order-by-length query?

.... order by length(t.iDescription)

Tom
Re: JPA: order by length? [message #385423 is a reply to message #385414] Mon, 02 February 2009 13:39 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I don't think JPQL allows this, but you can do it through EclipseLink's
query API.

ExpressionBuilder builder = new ExpressionBuilder();
ReadAllQuery query = new ReadAllQuery(YourClass.class, builder);
query.setSelectionCriteria(builder.get("iDescription").like( "%something%"));
query.addOrdering(builder.get("iDescription").length());

Query jpaQuery =
((JpaEntityManager)entityManager.getDelegate()).createQuery( query);


Please vote for this bug to get these functions added to JPQL in
EclipseLink:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=219814

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Re: JPA: order by length? [message #385424 is a reply to message #385423] Mon, 02 February 2009 15:21 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
> Query jpaQuery =
> ((JpaEntityManager)entityManager.getDelegate()).createQuery( query);


Do I need a specific version of EclipseLink? The one I use (1.0.2) does not have a createQuery method that accepts a ReadAllQuery as a parameter.

Tom
Re: JPA: order by length? [message #385425 is a reply to message #385424] Mon, 02 February 2009 15:31 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
I used a Collections.sort on the result to order by length. That'll do fine.
Re: JPA: order by length? [message #385430 is a reply to message #385424] Tue, 03 February 2009 12:54 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Might only be in 1.1, in 1.0 you can use,

Query jpaQuery = entityManager.createQuery("Select a from MyClass a");
((JpaQuery)query).setDatabaseQuery(query);


James : Wiki : Book : Blog : Twitter
Previous Topic:Replacing collections
Next Topic:not so lazy / locking a record
Goto Forum:
  


Current Time: Sun Nov 23 08:54:42 GMT 2014

Powered by FUDForum. Page generated in 0.02065 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software