java - Mapping multiple collections in one entity in Hibernate -


i quite new hibernate , programming databases in general honest... i've tried save graph-like structure database.

suppose have java class this:

public class user {     @onetomany(cascade=cascadetype.all)     private collection<user> followers = new arraylist<>();     @onetomany(cascade=cascadetype.all)     private collection<user> friends = new arraylist<>();     @id     private string name;     ..... } 

the problem want save postgesql database using hibernate. found quite nontrivial. 1 problem example is: suppose do:

user user1 = new user("user1"); user user2 = new user("user2"); user1.getfollowers().add(user2); user1.getfriends().add(user2); 

now if merge on user1 object there issue key uniqueness constraint. wonder if issue because misconfigured hibernate annotations save structure or entirely wrong approach represent graph in such way using hibernate ? appreciated.

personally go generated id column, , not use user's name that. put constraint on name column in database rather.

@id  @generatedvalue(strategy=generationtype.identity)  @column private long id; 

otherwise cannot see wrong approach. exceptions while running code?


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -