java - Criteria Query 2, query through 3 entities level -
i learning criteria api, encounter issue create query. here structure :
an admin has list of groups, group has list of admins (so manytomany relationship)
a group has list of companies, company has 1 group (so onetomany)
to find groups of admin created request :
@override public list<group> getallgroupsmanagedbyadmin(admin admin) { final criteriabuilder cb = entitymanager.getcriteriabuilder(); final criteriaquery<group> query = cb.createquery(group.class); final root<admin> admins = query.from(admin.class); query.where(cb.equal(admins.get(admin_.id), admin.getid())) .select(admins.join(admin_.groups)); return this.entitymanager.createquery(query).getresultlist(); }
now i'm trying find companies of 1 group sure group managed admin provided in parameter, method definition :
public list<company> getcompaniesbygroupidmanagedbyadmin(string groupid, admin admin)
but drafts failed @ moment. give me ? thank !
finally find myself, solution easier expected :
public list<company> getcompaniesbygroupidmanagedbyadmin(string groupid, admin admin) { final criteriabuilder cb = entitymanager.getcriteriabuilder(); final criteriaquery<company> query = cb.createquery(company.class); final root<admin> admins = query.from(admin.class); final join<admin, group> groups = admins.join(admin_.groups); final predicate[] predicates = new predicate[2]; predicates[0] = cb.equal(admins.get(admin_.id), admin.getid()); predicates[1] = cb.equal(groups.get(group_.id), groupid); query.where(predicates) .select(groups.join(group_.companies)); return this.entitymanager.createquery(query).getresultlist(); }
Comments
Post a Comment