1

I would like to order a query by a function which uses properties of the class. Something like:

@entity
class A
{
private int id;
private String name;

public int StringLength()
{
return name.length();
}

}

And the sentence something like: select n from A order by name.StringLength(). Is it possible?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Pablo Castilla
  • 2,723
  • 2
  • 28
  • 33

2 Answers2

1

No you can't, but if you want to query based on the length of one of the property you can use the length function similar to SQL:

select n from A order by length(name)
David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
0

David's answer was right - you cannot call the function - but in EJBQL the query should look something like this:

select n from A n order by length(n.name)
tputkonen
  • 5,579
  • 16
  • 60
  • 88