java - nonuniqueobjectexception when Updating Database and Database creating new records instead of updating existing ones -
i'm having issue don't understand, since started working databases. keep getting nonuniqueobjectexception, when try save 2 instances of project database. when save session project keep information , update database accordingly inspection table connected project database continue add new entries. tried multiple fixes of trying throw exceptions can't figure out way save information in table without creating new entries in instance table. can give me advice on how tackle issue , look?
public static void editproject(long warehouseid, long managerid, long supervisorid, long classid, long projectitemid, long statusid, long stageid, long typeid, string scope, map<string, string>params, long inspectiontn) throws classnotfoundexception, parseexception { //initialize services dateformat formatter = new simpledateformat("mm/dd/yyyy"); //get essential project data warehouse warehouse = (warehouse) projectobjectservice.get(warehouseid, "warehouse"); person manager = (person) projectobjectservice.get(new long(managerid), "person"); person supervisor = (person) projectobjectservice.get(new long(supervisorid), "person"); projectclass projectclass = (projectclass) projectobjectservice.get(new long(classid), "projectclass"); projectstatus status = (projectstatus) projectobjectservice.get(new long(statusid), "projectstatus"); projectitem item = (projectitem) projectobjectservice.get(new long(projectitemid), "projectitem"); projectstage stage = (projectstage) projectobjectservice.get(stageid, "projectstage"); projecttype ptype = (projecttype) projectobjectservice.get(typeid, "projecttype"); string mcsnumstring = params.get("mcsnumber"); //id's string iidstring = params.get("inspectionid"); string closeoutidstring = params.get("closeoutid"); int mcsnum = -1; long iid = (long)-1; long closeoutid = (long)-1; //parse mcsnumber, change -1 if it's not number try { mcsnum = integer.parseint(mcsnumstring); iid = long.parselong(iidstring); closeoutid = long.parselong(closeoutidstring); }catch(exception e){} //inspections inspections=(inspections) projectobjectservice.get(iid, "inspections"); int shouldinvoice = integer.parseint(params.get("shouldinvoice")); int actualinvoice = integer.parseint(params.get("actualinvoice")); string notes = params.get("notes"); //additional fields string zachnotes = params.get("zachupdates"); string cost = params.get("cost"); string customernumber = params.get("customernumber"); //parse change orders strong string changeorderjsonstring = params.get("coitems"); changeorderservice orderservice = new changeorderservice(); hashset<changeorder> changeorders = changeorderservice.getchangeordersfromstring(changeorderjsonstring); date fsalvagedate = null; date finitiateddate = null; date fsurvey = null; date fcostco = null; date fproposal = null; date fasbuilts = null; date fpunchlist = null; date falarmhvac = null; date fverisae = null; date fcloseoutbook = null; date fcloseoutnotes = null; date fstart = null; date fscheduled = null; date factual = null; date fairgas = null; date fpermits = null; date permitapp = null; date fframing = null ; date fceiling = null; date froughmech = null; date froughelec= null; date froughplumb = null; date fmechlightsmoke = null; date fmechfinal = null; date felecfinal = null; date fplumbfinal = null; date ffiremarshal = null; date fhealth = null; date fbuildfinal = null; //assign values dates, if not null if (!(params.get("salvagedate")).isempty()) fsalvagedate = formatter.parse(params.get("salvagedate")); if (!(params.get("initiated")).isempty()) finitiateddate = formatter.parse(params.get("initiated")); if (!(params.get("survey")).isempty()) fsurvey = formatter.parse(params.get("survey")); if (!(params.get("costco")).isempty()) fcostco = formatter.parse(params.get("costco")); if (!(params.get("proposal")).isempty()) fproposal = formatter.parse(params.get("proposal")); if (!(params.get("asbuilts")).isempty()) fasbuilts = formatter.parse(params.get("asbuilts")); if (!(params.get("punchlist")).isempty()) fpunchlist = formatter.parse(params.get("punchlist")); if (!(params.get("alarmhvac")).isempty()) falarmhvac = formatter.parse(params.get("alarmhvac")); if (!params.get("verisae").isempty()) fverisae = formatter.parse(params.get("verisae")); if (!params.get("startdate").isempty()) fstart = formatter.parse(params.get("startdate")); if (!params.get("scheduledturnover").isempty()) fscheduled = formatter.parse(params.get("scheduledturnover")); if (!params.get("actualturnover").isempty()) factual = formatter.parse(params.get("actualturnover")); if (!params.get("airgas").isempty()) fairgas = formatter.parse(params.get("airgas")); if (!params.get("permits").isempty()) fpermits = formatter.parse(params.get("permits")); if (!params.get("permitapp").isempty()) permitapp = formatter.parse(params.get("permitapp")); if(!params.get("framing").isempty()) fframing = formatter.parse(params.get("framing")); if(!params.get("ceiling").isempty()) fceiling = formatter.parse(params.get("ceiling")); if(!params.get("roughmech").isempty()) froughmech = formatter.parse(params.get("roughmech")); if(!params.get("roughelec").isempty()) froughelec = formatter.parse(params.get("roughelec")); if(!params.get("roughplumb").isempty()) froughplumb = formatter.parse(params.get("roughplumb")); if(!params.get("mechlightsmoke").isempty()) fmechlightsmoke = formatter.parse(params.get("mechlightsmoke")); if(!params.get("mechfinal").isempty()) fmechfinal = formatter.parse(params.get("mechfinal")); if(!params.get("elecfinal").isempty()) felecfinal = formatter.parse(params.get("elecfinal")); if(!params.get("plumbfinal").isempty()) fplumbfinal = formatter.parse(params.get("plumbfinal")); if(!params.get("firemarshal").isempty()) ffiremarshal = formatter.parse(params.get("firemarshal")); if(!params.get("health").isempty()) fhealth = formatter.parse(params.get("health")); if(!params.get("buildfinal").isempty()) fbuildfinal = formatter.parse(params.get("buildfinal")); closeoutdetails cd = null; if(cd==null) { cd = new closeoutdetails(); system.out.println("closeoutdetails empty in edit"); } //closeout fields cd.setpunchlist(fpunchlist); cd.setasbuilts(fasbuilts); cd.setairgas(fairgas); cd.setalarmhvacform(falarmhvac); cd.setcloseoutbook(fcloseoutbook); cd.setcloseoutnotes(fcloseoutnotes); cd.setpermitsclosed(fpermits); cd.setpunchlist(fpunchlist); cd.setverisaeshutdownreport(fverisae); //need add salvage amount , date inspections inspections = null; //create inspections object. if(inspections==null) { inspections = new inspections(); system.out.println("inpsections empty in edit"); } //set inspection fields inspections.setticketnumber(inspectiontn); inspections.setframing(fframing); inspections.setceiling(fceiling); inspections.setroughin_mechanical(froughmech); inspections.setroughin_electric(froughelec); inspections.setroughin_plumbing(froughplumb); inspections.setmechanicallightsmoke(fmechlightsmoke); inspections.setmechanical_final(fmechfinal); inspections.setelectrical_final(felecfinal); inspections.setplumbing_final(fplumbfinal); inspections.setfire_marshal(ffiremarshal); inspections.sethealth(fhealth); inspections.setbuilding_final(fbuildfinal); /*try{ inspections.setid(iid); projectobjectservice.editobject("inspections",iid,inspections); }catch (nonuniqueobjectexception nuoe){}*/ //create new project replace old 1 project p = new project(); p.setmcsnumber(mcsnum); p.setprojectclass(projectclass); p.addprojectmanager(manager); p.addsupervisor(supervisor); p.setstatus(status); p.setwarehouse(warehouse); p.setscope(scope); p.setprojectitem(item); p.setstage(stage); p.setprojectinitiateddate(finitiateddate); p.setsitesurvey(fsurvey); p.setcostcoduedate(fcostco); p.setproposalsubmitted(fproposal); p.setscheduledstartdate(fstart); p.setscheduledturnover(fscheduled); p.setactualturnover(factual); p.setshouldinvoice(shouldinvoice); p.setinvoiced(actualinvoice); p.setprojectnotes(notes); p.setchangeorders(changeorders); p.setprojecttype(ptype); p.setzachupdates(zachnotes); p.setcost(cost); p.setcustomernumber(customernumber); p.setpermitapplication(permitapp); p.setcloseoutdetails(cd); p.setinspections(inspections); //replace old project new project. long id = long.parselong(params.get("projectid")); //projectobjectservice.editobject("project", id,p); long[] ids = {iid,/*closeoutid,*/id} ; string[] domains = {"inspections",/*"closeoutdetails",*/"project" }; projectobject[] objects ={inspections, /*cd,*/ p}; inspections.setid(iid); projectobjectservice.editobject("project",id,p); } public static object get(long id, string domain) throws classnotfoundexception { session session = hibernateutil.getsessionfactory().getcurrentsession(); transaction tx = session.begintransaction(); //get class parsing "domain" string. class c = class.forname("projectobjects."+domain); //get object database matches id object o = session.get(c, id); tx.commit(); return o; }
Comments
Post a Comment