hibernate - JPA criteria query, order on class -


is there way jpa criteria queries order on class? imagine following domain objects:

abstract class hobby { ... } class coding extends hobby { ... } class gaming extends hobby { ... } 

using regular ql able do

from hobby h order h.class 

but when apply same logic on criteria query, runtime exception "unknown attribute" occurs.

criteriaquery<hobby> criteriaquery = builder.createquery(hobby.class); root<hobby> hobbyroot = criteriaquery.from(hobby.class); criteriaquery.orderby(builder.asc(hobbyroot.get("class")); list<hobby> hobbies = entitymanager.createquery(criteriaquery).getresultlist(); 

jpa implementation used: hibernate-entitymanager v3.5.5-final

jpa 2.0 introduces new type expression allow query restrict results based on class types.

you can use type expression criteria api using path#type(). try:

criteriaquery criteriaquery = builder.createquery(hobby.class); root hobbyroot = criteriaquery.from(hobby.class); criteriaquery.orderby(builder.asc(hobbyroot.type()); list hobbies = entitymanager.createquery(criteriaquery).getresultlist();

while code compiles, didn't test (i'll give try tomorrow).

actually, wonder if legal or if type() should part of select in order order by (maybe that's criteria query supposed generate). need check that.

references

  • jpa 2.0 specification
    • section 4.6.17.4 "entity type expressions"

more resources


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -