Take the 2-minute tour ×

I have these twos sql scripts, and i'm getting some problems to perform the translation in hql, to be used in a Query from JPA:

SELECT f.idprovider, pe.name from provider f
inner join person pe
on(f.idperson = pe.idperson) 
inner join product_provider pf
on(f.idprovider = pf.idprovider) 
inner join product p
on(pf.idproduct = p.idproduct)
inner join item i
on(p.idproduct = i.idproduct)
where i.iditem = 1

SELECT f.idprovider, pe.name, pe.cnpjf from product_provider pf
inner join provider f  
on(pf.idprovider = f.idprovider)
inner join coliseusys.person pe
on(f.idperson = pe.idperson)
inner join product p
on(pf.idproduct = p.idproduct)
where p.idproduct = (select p2.idproduct from item i inner join product p2
on(i.idproduct = p2.idproduct) where i.iditem = 1)

these sqls do the same thing, and i've tried a lot do take the hql from any of it, without results. the mapping in hibernate is ok.

any idea?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

The translation is immediate :

select f.id, pe.name from Provider f
inner join f.person pe
inner join f.products p
inner join p.items i
where i.id = :itemId

This is assuming that

  • Provider has a toOne association with Person
  • Provider has a toMany association with products, using the join table product_provider
  • Product has a OnetoMany association with Item

If that's not the case, then describe your mapping, because doing HQL without knowing the entities is impossible. It's like doing SQL without knowing the tables.

You should read http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql. It has lots of examples of HQL queries and explain how they work in great details.

share|improve this answer
ok, man, thanks for your attencion. The problem was solved by adding a reference in product. And sorry, my english ins't very good –  lucianohdr Oct 25 '11 at 16:30
add comment

Your Answer


