Hibernate Query with conditions in a complex db structure

I have a complex database that looks like this:

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

Now I would like to request, for example. all products where user.firstname = 'peter' in hql.

+3
source share
2 answers

I figured out how to handle this:

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
+3
source

In your entities and mappings, you must have references for each of these relationships. And your HQL query will look like this:

SELECT p FROM Product p, IN(p.inventory.inventoryUser) AS iu 
    WHERE iu.username=:username
+2
source

Source: https://habr.com/ru/post/1730845/


All Articles