How to add an object to existing List using Spring Mongo db? -


this class a

@document class user{ private string id ; private string name; @dbref private list<socity> socitys; } 

and class socity

@document class socity{ private string id ; private string name; } 

and add user function

public user adduser(user user) {     list<socity> socity = new arraylist<>();     user.setsocitys (socity );     return  userrepository.save(user); } 

i want add socity existing user try doesn't work

public static void main(string[] args) {     applicationcontext ctx  = springapplication.run (app.class, args);     socitydao sdao = ctx.getbean(socitydao .class);     userrepository userrepository = ctx.getbean(userrepository.class);      user u = userrepository.findone("");     socity s = new socity("soc1");     sdao .addsocity(e);     u.getsocitys().add(e);  } 

this rest service

@requestmapping(value = "up/{id}", method = requestmethod.post ,produces =  "application/json") public user addsocitytouser(@requestbody socity, @pathvariable  string id)  {       return socitydao.addsocitytouser(e, id); } 

in end of code add userrepository.save(u) persist changes db.

as long as id (because persisted object) updated. if has no id saved new object in db.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -