更新数据库和数据库创建新记录而不是更新现有记录时的非唯一性错误

时间:2015-06-02 16:57:48

标签: java mysql hibernate sql-update

我遇到了一个我不太懂的问题,特别是因为我刚开始使用数据库。当我尝试将项目的两个实例保存到数据库中时,我不断得到一个非唯一的例子。当我保存Project的会话时,它将保留所有信息并相应地更新数据库,但连接到Project数据库的Inspection表将继续添加新条目。我试图尝试抛出异常的多个修复,但我无法找到一种方法来保存表中的信息而不在实例表中创建新条目。谁能给我任何关于如何解决这个问题以及在哪里看的建议?

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 to -1 if it's not a 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 from 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 to dates, if they are 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 was 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 to add salvage amount and date

    Inspections inspections = null;
    //create inspections Object.
    if(inspections==null)
    {
         inspections = new Inspections();
         System.out.println("Inpsections was 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 to replace the old one
    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 the old project with the 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 the Class from parsing the "domain" string.
    Class c = Class.forName("projectObjects."+domain);

    //Get object from database that matches the id
    Object o = session.get(c, id);
    tx.commit();
    return o;
}

0 个答案:

没有答案