views:

107

answers:

2

I have a complex database that's looking like this:

product *1 <-> n* inventory *n <-> 1* inventoryUser *1 <-> n* user

Now I would like to query e.g. all products where user.firstname = 'peter' in hql.

A: 

In your entities and mappings you should have references for each of these relations. And your HQL query will look like:

SELECT p FROM Product p, IN(p.inventory.inventoryUser) AS iu 
    WHERE iu.username=:username
Bozho
hehe if it would only be that easy... I have tried this, but because of the fact that inventoryUser is a Collection Hibernate throws a 'QueryException: illegal attempt to dereference collection'
woezelmann
updated. I haven't tried it, but a join is the way to go
Bozho
no, still getting: 'HibernateQueryException: illegal attempt to dereference collection [product0_.id.inventories] with element property reference [inventoryUser]'
woezelmann
now? (after the last update)
Bozho
+1  A: 

I figered out how to handle it:

Product as p join fetch p.inventories as i join fetch i.inventoryUser as iu join fetch iu.user as u where u.name=:name
woezelmann