database - how to add other columns to the joinTable in a Many To Many relationship in hibernate JPA? -
i have 2 entities user , file . file can shared many users , user can receive many files . it's many many relationship between 2 entities .
in user class :
@manytomany @jointable(name = "receiver_shared", joincolumns = @joincolumn(name = "user_id", insertable =false, updatable = false), inversejoincolumns = @joincolumn(name ="file_id", insertable = false, updatable = false)) private list<file> receivedfiles;
in file class :
@manytomany(mappedby = "receivedfiles") private list<user> receivers;
my problem want save sharetime , assign permissions when file shared user . mean file 1 can shared user 1 permissions read , write , it's shared user 2 permission read .
i'm new jpa . thinking receiver_shared table must contain permission column , sharetime column .
how ?
update :
as jb nizet suggested i've created new share entity .
i've added file class
@onetomany (mappedby = "sharedfile",fetch=fetchtype.lazy) private list<share> shares = new arraylist<share>();
i've added user class
@onetomany (mappedby = "receiver",fetch=fetchtype.lazy) private list<share> receivedfiles = new arraylist<share>();
in share class have added column sharetime , column permission , :
@manytoone(fetch=fetchtype.eager) @joincolumn(name = "receiver_id") private user receiver; @manytoone(fetch=fetchtype.eager) @joincolumn(name = "file_id") private file sharedfile;
i tested configuration junit
user receiver=userrepository.findone((long)2); file toshare=filerepository.findone((long)2); share s=new share(toshare,receiver,1,new timestamp(newdate().gettime())); sharerepository.save(s);
if join table contains additional functional information, it's not join table anymore, , must mapped entity.
so you'll have one-to-many between user , share, , one-to-many between file , share.
Comments
Post a Comment