How to setup a basic manytomany relationship with JPA/Hibernate? -


i have basic many many relationship: persons , projects, set follows:

person:

@manytomany(cascade=cascadetype.all) @jointable(name = "person_project",  joincolumns = @joincolumn(name = "person_id"), inversejoincolumns = @joincolumn(name = "project_id")) private set<project> projects = new hashset<project>(); 

project:

@manytomany @jointable(name = "person_project",  joincolumns = @joincolumn(name = "project_id"),  inversejoincolumns = @joincolumn(name = "person_id")) private set<person> persons = new hashset<person>(); 

when updating person, new projects may specified. have jparepository set person , use saveandflush(...) save possible new projects person.

this works 1 person. if add new project saved person , tables in database update correctly. however, if add same project person - deleted first person! id of first person replaced id of second person in join table.

what causes this? relation setup incorrect?

you don't use same annotation on both halves of mapping. first part defines join tables , columns. second half of mapping needs know name of other half (projects in case), details first annotation. decent explaination see: hibernate many many mapping


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -