0

i have to dispaly all the subcategories of each category in my jsf page using JPQL. i have a table of Category( idCategory, name, idParent). How can i manage the jpql query, in order to show me ALL the Hierarchy of Category??

the SQL query is like bellow:

select root.name  as root_name
 , down1.name as down1_name
 , down2.name as down2_name
 , down3.name as down3_name

   from  category as root
   left outer join category as down1
     on down1.parent = root.category_id
   left outer join category as down2
     on down2.parent = down1.category_id
   left outer join category as down3
     on down3.parent = down2.category_id
   where root.parent is null  

   order by 
      root_name 
    , down1_name 
    , down2_name 
    , down3_name
  ;

thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Check this: http://stackoverflow.com/questions/3638082/recursive-jpa-query – Multisync Oct 18 '14 at 23:34
  • However, if your database supports hierarchical queries (Oracle for example) you may write a native query (but it's not a "good way"). If you know exactly how many levels of hierarchy can exist in your entity (like 3 in the example) it's possible to use JPAQL. Is it your case? – Multisync Oct 18 '14 at 23:41
  • thks for your fast reply, Yes! i know the levels of my hierarchy. so how can i get the result , pls? – user1935532 Oct 19 '14 at 00:07
  • Sorry, I thought you need SQL, but you have the correct one. Have you tried this? http://stackoverflow.com/questions/7847972/jpa-join-entity-on-the-same-entity – Multisync Oct 19 '14 at 01:12

0 Answers0