Could I order in EJBQL by a function which is not mapped to a column in database?

521 Views Asked by At

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?

2

There are 2 best solutions below

0
David Rabinowitz On BEST ANSWER

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)
0
tputkonen On

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)