java - Update not Working in Hibernate many to Many Relational Table -


i trying design crud controller, in here insert , delete operation working update isn't working. when submitting updating, user table updated role table isn't updated.

user.java

@entity @table(name="users") public class user implements userdetails {     @id     @generatedvalue (strategy = generationtype.auto)     private long id;      @column(length = 128, nullable = false)     private string firstname;       @column(length = 128, unique = true, nullable = false)     private string email;                       // required; unique      @column(unique = true, nullable = false, length=128)     private string name;      @column(length = 128, nullable = false)     private string password;                    // required      @column     private boolean enabled;      @jsonbackreference     @manytomany(fetch = fetchtype.eager, cascade = cascadetype.all)     @jointable(             name = "company_user",             joincolumns = @joincolumn(name = "userid"),             inversejoincolumns = @joincolumn(name = "companyid")     )     private set<company> companies = new hashset<company>();      @manytomany(fetch = fetchtype.eager, cascade = {cascadetype.merge})     @jointable(             name = "user_role",             joincolumns = @joincolumn(name = "userid", referencedcolumnname="id"),             inversejoincolumns = @joincolumn(name = "roleid", referencedcolumnname="id")     )     private set<role> roles = new hashset<role>(); 

role.java

@entity public class role extends baseobject {     @column(length = 64)     private string description; 

usercontroller.java

@postmapping("/saveuser")         public void save(@modelattribute user user, bindingresult bindingresult, httpservletrequest req, httpservletresponse rsp) throws ioexception {              user.setpassword(new bcryptpasswordencoder().encode(user.getpassword()));         //  logger.info("--------------role user id----------" + user.getroles());             user.setroles(user.getroles());             userservice.registeruser(user);              req.setattribute("users", userservice.findallusers());             req.setattribute("mode", "user_view");             rsp.sendredirect("showuser");         }   **userservice.java**   public user registeruser(user user) {         optional<user> exists = userrepository.findonebyname(user.getname());         return exists.ispresent() ? userrepository.save(exists.get().merge(user))                 : userrepository.save(user);     } 

how can overcome situation? please me.....

try use cascade = cascadetype.all on field
private set roles = new hashset();

i assuming ispresent returning false , userrepository.save(user) running save users not roles using cascade = {cascadetype.merge} potentially solve problem.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -