sql - JPA with Hibernate - Many to Many relationship, fetching all data -
i have many many relation ship between user , roles. example
public class user { @id private integer id; @manytomany @jointable(name = "app_user_role", joincolumns = { @joincolumn(name = "user_id") }, inversejoincolumns = { @joincolumn(name = "role_id") }) private list<role> roles = new arraylist<role>(); } @entity public class role { @id private integer id; @manytomany(mappedby = "roles") private list<user> users = new arraylist<user>(); }
my requirement fetch users in scheme roles. when using hql using query
select u, u.roles user u
however fires query every user corresponding roles. if there 100 users, fire 100 queries. in sql can accomplish using follwing query
select u.id, u.name, m.roleid, r.name user u left outer bring together userrole m on u.id = m.userid inner bring together roles r on m.roleid = r.id order u.id;
i doing left outer bring together user there user have no associated roles. prefer not utilize native sql has own disadvantages. 1 help me map corresponding hql or jpql query?
try query
select u user u left bring together fetch u.roles
sql hibernate jpa many-to-many hql
No comments:
Post a Comment