java - JPA Query from 3 column join table -
i have following entities. means user can belong many businesses , each business user can has separate permissions. existing user can assigned business. business_user table looks this:
user_id business_id authority_name 6 1 role_analytics 6 1 role_self_analytics 7 1 role_reviews 8 1 role_analytics 8 1 role_self_analytics 8 1 role_reviews 6 2 role_reviews 6 2 role_self_analytics
question: querying users 1 business trying build list of user dto objects, dto exposes list<authority>
, problem can't figure how should these authorities each user business_user table. been trying fancy stuff lambdas have no luck. using spring data queries, maybe can solve in repository.
thanks in advance.
edit: if go route adding new join table business_user_authority
, how have describe in userbusiness
class? primary key on user_id , business_id. note name
pk in authority
table.
something this, not create me join table @ all.
change userbusiness class:
@manytomany(fetch = fetchtype.eager) @jointable( name = "business_user_authority", joincolumns = {@joincolumn(name = "business_user_id", referencedcolumnname = "id")}, inversejoincolumns = {@joincolumn(name = "authority_name", referencedcolumnname = "name")}) private set<authority> authorities = new hashset<>();
user
@entity @table(name = "user") public class user extends abstractauditingentity implements serializable { @id @generatedvalue(strategy = generationtype.auto) private long id; @onetomany(cascade = cascadetype.all) @joincolumns({ @joincolumn(name = "user_id", referencedcolumnname = "id") }) private set<businessuser> businessusers = new hashset<>(); }
businessuser
@entity @table(name = "business_user") @idclass(businessuser.class) public class businessuser implements serializable { @id @manytoone(fetch = fetchtype.eager) @joincolumn(name = "user_id") private user user; @id @manytoone(fetch = fetchtype.eager) @joincolumn(name = "business_id") private business business; @id @manytoone(fetch = fetchtype.eager) @joincolumn(name = "authority_name") private authority authority; }
business
@entity @table(name = "business") public class business implements serializable { @onetomany(cascade = cascadetype.all) @joincolumns({ @joincolumn(name = "business_id", referencedcolumnname = "id") }) private set<businessuser> businessusers = new hashset<>(); }
authority
@entity @table(name = "authority") public class authority implements serializable { @notnull @size(min = 0, max = 50) @id @column(length = 50) private string name; }
i ended writing function that, if has solution how design join table between businessuser <--> businessuserauthority i'd glad implement it.
private function<map.entry<user, list<businessuser>>, usermanagementdto> getpermissionforuser() { return user -> { usermanagementdto dto = usermanagementmapper.usertousermanagementdto(user.getkey()); dto.setauthorities(user.getvalue().stream().map(businessuser::getauthority).collect(collectors.toset())); return dto; }; }
Comments
Post a Comment