java - How to fetch many to one and many to many by one request? -
i have 4 tables, example:
- admin (id, email, role) - many 1
- role (id, rights) - 1 many
- right (id, roles) - 1 many
- role_right (role, right) - many many
i need admin email fetching role , list of rights in role jpa named queries. know how 2 queries want 1 sql request.
for example this:
select admin admin admin join fetch admin.role role {maybe there need fetch rights} admin.email = :email thank helping!
update:
ok, found solution. set annotations table roles:
@entity @table(name = "hls_admin_role", schema = "public") public final class adminrole implements serializable { ... @manytomany @jointable(name = "hls_admin_role_right", joincolumns = { @joincolumn(name = "role_id") }, inversejoincolumns = { @joincolumn(name = "right_id") }) private list<adminright> rights = new arraylist<>(); ... } set annotations table rights:
@entity @table(name = "hls_admin_right", schema = "public") public final class adminright implements serializable { ... @manytomany(mappedby = "rights") private list<adminrole> roles = new arraylist<>(); ... } after works fine with:
@repository public interface adminrepository extends baserepository<admin, long> { @query("select admin admin admin " + "inner join fetch admin.role role "+ "inner join fetch role.rights rights " + "where admin.email = :email") admin findbyemailfetchall(final @param("email") string email); } maybe have best solution.
Comments
Post a Comment