hibernate - Insertion happened when update the record -


in jsf project seam3.2.2 integration, update doesn't work. instead update value, insert new value in table.

please need here.

flow of project.
pages.xml:

    <page  view-id="/updateuser.xhtml" action="#{usereditaction.setcurrentuser()}">         <navigation>             <rule if-outcome="success">                 <redirect view-id="/secure/admin/user/updateuser.xhtml"/>              </rule>         </navigation>         <begin-conversation join="true"></begin-conversation>     </page> 

usereditaction.java bean:

    @requestparameter     private integer userid;      @override     public string setcurrentuser(){         system.out.println("selected  userid ----- " + userid);         currentuser = userservice.getuser(userid);         return "success";     } 

userupdate.xhtml :

        <h:commandbutton value="update" tabindex="20"             action="#{useraction.update}">             <f:param name="userid" value="#{currentuser.userid}" />         </h:commandbutton> 

useraction.java bean

@name("useraction") @stateless @autocreate public class useractionimpl implements useraction {      @out(value = "currentuser", scope = scopetype.conversation)     @in(value = "currentuser", scope = scopetype.conversation, required = false)     private clrpuser currentuser;         @in     private userservice userservice;          @requestparameter     private integer userid;      private static final long serialversionuid = 8282995226262125676l;      public string update() {         userservice.saveorupdate(currentuser);         return "success";     } 

userservice.java:

    public integer saveorupdate(clrpuser user) {         // todo validation...check cell phone number.. email id..         // user.setlastupdatedby(currentuserid);         user.setlastupdatedby(loggedinuser.getid());         user.getresidentialaddress().setlastupdatedby(loggedinuser.getid());         user.getshippingaddress().setlastupdatedby(loggedinuser.getid()); 

usermapper.java :

public class usermapper {      public static userentity mapuserentity(clrpuser user) {         userentity userentity = new userentity();          userentity.setemailaddress(user.getemailaddress());         userentity.setfirstname(user.getfirstname());         userentity.setlastname(user.getlastname());         userentity.sethomenumber(user.gethomenumber());         userentity.setofficenumber(user.getofficenumber());         userentity.setcellnumber(user.getcellnumber());         auditinfo auditinfo = new auditinfo();         auditinfo.setlastupdatedby(user.getlastupdatedby());         auditinfo.setlastupdatedtime(user.getlastupdatedtime());         userentity.setauditinfo(auditinfo);         userentity.settype(user.gettype());         userentity.setpreferenceid(user.getpreferenceid());         userentity.setaspcustomerid(user.getaspcustomerid());          user.getresidentialaddress().setaddresstype("r");         user.getshippingaddress().setaddresstype("s");         addressentity residential = addressmapper.mapaddressentity(user                 .getresidentialaddress());         addressentity shipping = addressmapper.mapaddressentity(user                 .getshippingaddress());         list<addressentity> addressess = new arraylist<addressentity>();         addressess.add(residential);         addressess.add(shipping);          userentity.setaddresses(new hashset<addressentity>(addressess));          /*userloginentity userlogin = userloginmapper.mapuserloginentity(user.getuserlogin());         list<userloginentity> userloginn = new arraylist<userloginentity>();         userloginn.add(userlogin);          userentity.setuserlogin(new hashset<userloginentity>(userloginn));*/         // userentity.setcardtypeentity(cardtypeentity)         return userentity;     } 

jpauserdao.java

    public integer saveorupdate(userentity newuser) {                entitymanager.merge(newuser);         return newuser.getuserid();     } 

issue:-
insertion happening because on update everytime creating new instance of userentity entity , not setting id or primary key. on saving hibernate thinks entity new , new id gets generated everytime causing insertion

solution:-
correct add below line in mapuserentity() method

userentity.setid(user.getid()); 

so mapuserentity() like

public static userentity mapuserentity(clrpuser user) {         userentity userentity = new userentity();         userentity.setid(user.getid());//note:-added line         userentity.setemailaddress(user.getemailaddress());         userentity.setfirstname(user.getfirstname());         userentity.setlastname(user.getlastname()); ... } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -