java - OneToMany mapping on Embeddable key -


i attempting build permission rule accessing items (stored in table tab) users (stored in table users). availability whether see stored in permissions_tabs table.

the result should if statement run (for user id=2):

select project.tab.tab_id, project.tab.parent, project.tab.name project.tab inner join project.permissions_tabs on project.tab.tab_id=project.permissions_tabs.tab_id permissions_tabs.user_id=2 , permissions_tabs.view=true; 

i attempting via @onetomany annotation, fails - receive message:

deployment failed. message was: exception description: @joincolumns on annotated element [field allowedtabs] entity class [class com.jtsmr.scheduler.persistence.entities.usersentity] incomplete. when source entity class uses composite primary key, @joincolumn must specified each join column using @joincolumns. both name , referencedcolumnname elements must specified in each such @joincolumn.

before amending db , adding permissions_tab, worked. when added - unable write correct @onetomany annotation, neither succeeded in finding manual case.

if not possible, solution via jpql suffice (i unable construct join operation).

here setup:

db (creation statements easier understanding):

create table `users` (   `user_id` bigint(20) not null,   `username` varchar(45) not null,   `password` varchar(45) not null,   `name` varchar(45) not null,   `email` varchar(80) not null,   primary key (`user_id`),   unique key `user_id_unique` (`user_id`),   unique key `username_unique` (`username`) ); create table `tab` (   `tab_id` bigint(20) not null auto_increment,   `parent` bigint(20) not null default '0',   `name` varchar(45) not null,   primary key (`tab_id`) ); create table `permissions_tabs` (   `user_id` bigint(20) not null,   `tab_id` bigint(20) not null,   `to_view` bit(1) default b'0',   `to_edit` bit(1) default b'0',   primary key (`user_id`,`tab_id`),   key `user_id_idx` (`user_id`),   key `tab_id_idx` (`tab_id`),   constraint `tab_id` foreign key (`tab_id`) references `tab` (`tab_id`) on delete no action on update no action,   constraint `user_id` foreign key (`user_id`) references `users` (`user_id`) on delete no action on update no action ); 

the jpa mapping follows (functions omitted):
tabs:

@entity @table(name = "tab") public class tabentity implements serializable {     @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "tab_id", insertable = false, updatable = false)     private long tabid;     @column(name = "parent", insertable = false, updatable = false)     private long parent;     @column(name = "name", insertable = false, updatable = false)     private string name; } 

users:

@entity @table(name = "users") public class usersentity implements serializable {     @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "user_id")     private long userid;     @column(name = "username")     private string username;     @column(name = "password")     private string password;     @column(name = "name")     private string name;     @column(name = "email")     private string email;     @onetomany     @jointable(         name = "permissions_tabs",         joincolumns         = {             @joincolumn(name = "user_id", referencedcolumnname = "user_id")},         inversejoincolumns         = {             @joincolumn(name = "tab_id", referencedcolumnname = "tab_id")})     private list<permissionstabsentity> allowedtabs; } 

primary key permissionstabsentity class:

@embeddable public class permissionstabspk implements serializable {     @column(name = "user_id")     private long userid;     @column(name = "tab_id")     private long tabid; } 

and permissionstabsentity:

@entity @table(name = "permissions_tabs") public class permissionstabsentity implements serializable {     @embeddedid     private permissionstabspk id;     @column(name = "to_view")     private boolean toview;     @column(name = "to_edit")     private boolean toedit; } 

thank in advance!


Comments