Hibernate many-to-many HQL and Creiteria

I have the following scripts:

USERS 1.1 ROLE \*.\* MODULE \*.\* INTERFACE 

One "JOHN" user has one role, and a role has several modules, and one module has several interfaces. I used sleep annotation which works well. When I get the user object, I get the role and using the role object I get the list of modules, and in the modular object I get the interfaces.

In simple, when I get a user, I get interfaces that contain the user, but how can I get interfaces that are not related to the user through HQL and criteria. eg

I am retrieving IN and NOT IN SQL interface data, but I want to retrieve data using HQL and criteria.

Thanks at Advance.

+4
source share
1 answer

IN and NOT IN are also supported in HQL. In HQL ...

To find out all user-related interfaces, given the user ID:

 select ui from USERS u join u.role.module.interface ui where u.id =:userId; 

To find out all non- user interfaces given the user id:

 select i from INTERFACE i where i NOT IN (select ui from USERS u join u.role.module.interface ui where u.id =:userId); 
0
source

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


All Articles