java - Same values are getting added to db multiple times -


// logindao, logic save ui values db... new values getting added , old values replaced

system.out.println("in vendor registration i.e logindao class::::::::::"+vendorregistration.getvid()+""+""+vendorregistration.getfirstname());                 session session = getsession();                                                session.begintransaction();                                                //begin transaction session.                 query query = session.createquery("update vendorregistration set firstname =:firstname,lastname =:lastname,email =:email,password =:password,vid =:vid ");                   query.setparameter("firstname",vendorregistration.getfirstname());                 query.setparameter("lastname",vendorregistration.getlastname());                 query.setparameter("email",vendorregistration.getemail());                 query.setparameter("password",vendorregistration.getpassword());                 query.setparameter("vid",vendorregistration.getvid());                                int user = query.executeupdate();                   session.save(vendorregistration);                  system.out.println("user values ::::::::::::::::::"+user);                 session.gettransaction().commit();                            //here transaction complete commit data in db.                  session.close();    // controller class  @requestmapping(value = {"/signup"}, method = requestmethod.post)     public string saveregaction(@modelattribute("signup") @validated vendorregistration vendorregistration, model model,httpsession session,bindingresult bindingresult) throws ioexception     {         system.out.println("the object is:" + model);         if(bindingresult.haserrors())         {             logger.info("user details===========" +vendorregistration.getfirstname()+""+vendorregistration.getlastname()+""+vendorregistration.getvid()+""+vendorregistration.getemail()+""+vendorregistration.getpassword());             logger.info("returning home.jsp page");               model.addattribute("vendor", new vendorregistration());              return "signup";         }         loginservice.savenewuser(vendorregistration);         session.setattribute("vendorregistration", vendorregistration);          logger.info("in registration page........... save register action");         return "vendorlogin"; 

//values saving multiple times retaining old values , saving new values db

![this issue db][1]][1]

//pojo class

@entity @table(name = "vendorregistration") public class vendorregistration  {       @id     @column(name = "id")     @generatedvalue(strategy = generationtype.identity)       private long id;       @column(name = "vid")     private string vid;      @notblank     @column(name = "firstname")     private string firstname;      @column(name = "lastname")     private string lastname;      @column(name = "email")     @email(message="please enter valid email id")     private string email;      @column(name ="password")     private string password; //getters , setters 

there couple of things wrong/flawed in code. first controller, has wrong method signature.

first fix method signature, bindingresult must directly follow method argument applies to, in case @modelattribute annotated element.

@requestmapping(value = {"/signup"}, method = requestmethod.post) public string saveregaction(@modelattribute("signup") @validated vendorregistration vendorregistration, bindingresult bindingresult, model model,httpsession session,) throws ioexception { ... } 

the issue have database due implementation of service/dao method. first execute update query , insert it. happens first updated , after new record inserted. should have session.save nothing more , nothing less in method.

session session = getsession();                                session.begintransaction();                                                   session.save(vendorregistration); session.gettransaction().commit();                            session.close(); 

this remainder of code still flawed, might lead starvation of connection pool in case of errors/exceptions. should use try/catch/finally in code. (or better let spring using spring managed transactions).

final session session = getsession(); try {     session.begintransaction();     session.save(vendorregistration);     session.gettransaction().commit();                            } catch (exception e) {     session.gettransaction().rollback(); }  {            session.close(); } 

you need each , every method works session. if don't, session might hang around , use connection connection pool. if happens enouhg application die or start crawl.

better use spring managed transactions.

@transactional public savenewuser(vendorregistration vendorregistration) {     sessionfactory.getcurrentsession().save(vendorregistration); } 

spring manages session , transaction you.

pro tip: don't use plain hibernate use jpa , use spring data jpa it. need this.

interface vendorregistrationrepository extends jparepository<long, vendorregistration> {} 

no implementation, interface , managed you.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -