java - JPA Self Referencing Entities -
i have table called user this:
public class user { private long userid; private string username; //other fields; }
now lets user can have other user friends. how create relationship in jpa.
meanwhile doing in user table in database:
user_id username user_friends
and in user entity this:
public class user { private long userid; private string username; //other fields; @onetomany(cascade = cascadetype.all) @column(name = "user_friends") private list<userentity> friends; }
this doesn't work. so, how achieve in jpa? in advance!
after doing research , @florian schaetz, found solution self referencing entities. need user can have other user friends , user can friend other users. so, did this:
public class user { private long userid; private string username; //other fields; @jointable(name = "user_friends", joincolumns = { @joincolumn(name = "adding_user", referencedcolumnname = "user_id", nullable = false)}, inversejoincolumns = { @joincolumn(name = "added_user", referencedcolumnname = "user_id", nullable = false)}) @manytomany private collection<user> friends; @manytomany(mappedby = "friends") private collection<user> adduser; }
hope other people! again friends!
Comments
Post a Comment