简体   繁体   English

修改反映在本地参考中的类级别哈希图更改

[英]Modifying the class level hashmap changes reflecting in local reference

I am trying to save insurance details to database based on insuranceid or updating if the insuranceid is already exists in the database.我正在尝试根据保险 ID 将保险详细信息保存到数据库中,或者如果保险 ID 已存在于数据库中,则进行更新。 The problem is that when I am trying to save multiple insurance details it is giving ConcurrentModificationException.问题是,当我尝试保存多个保险详细信息时,它给出了 ConcurrentModificationException。 While debugging I found that 1st cycle it is ok that is 1st insurance details is successfully saving to database.在调试时,我发现第一个周期可以将第一个保险详细信息成功保存到数据库中。 But when next insurance details is storing it is giving the Exception.但是当下一个保险详细信息正在存储时,它会给出异常。 Another thing is that here I am modifying main HashMap(patientId,HashMap(insuranceId,InsuranceInfo)), and the changes are reflecting in local reference that is the id is adding to insuranceMap variable in the current method().另一件事是,在这里我正在修改 main HashMap(patientId,HashMap(insuranceId,InsuranceInfo)),并且更改反映在本地引用中,即 id 添加到当前 method() 中的 insuranceMap 变量。 Please try to help me请试着帮助我

this is my storing method...这是我的存储方法...

public void storeInsuranceInformationToDataBase(int patientId) throws SQLException
{
    //It gives arrayList of isuranceIds which are already in the data base.
    ArrayList<Integer> insuranceIdsListInDatabase = getInsuranceIdsListInDatabase();  

    //It returns HashMap of insurance ids for given patient id. 
    HashMap<Integer, InsuranceInfo> insuranceMap = getInsuranceDetails(patientId);   
    Set<Integer> insuranceIdsInHashMap = insuranceMap.keySet();

    //Here I am getting ConcurrentModificationException
    for (int insuranceIdInHashMap : insuranceIdsInHashMap)
    {
        //Here I am comparing the both ids..
        if (insuranceIdsListInDatabase.contains(insuranceIdInHashMap))  
        {
            String updateInsuranceQuery = "UPDATE jp_InsuranceInfo SET  typeOfPolicy='" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "', amount=" + insuranceMap.get(insuranceIdInHashMap).getAmount() + ", duration=" + insuranceMap.get(insuranceIdInHashMap).getDuration()
                    + ", companyName='" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "' WHERE insuranceId=" + insuranceIdInHashMap + ";";

            getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
        }
        else
        {
            String insertInsuranceQuery = "INSERT INTO jp_InsuranceInfo VALUES(" + insuranceMap.get(insuranceIdInHashMap).getPatientId() + ",'" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "'," + insuranceMap.get(insuranceIdInHashMap).getAmount() + ","
                    + insuranceMap.get(insuranceIdInHashMap).getDuration() + ",'" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "')";

            getDataBase().getStatement().executeUpdate(insertInsuranceQuery);

            ResultSet generatedInsuranceId = getDataBase().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
            int newInsuranceId = 0;
            if (generatedInsuranceId.next())
            {
                newInsuranceId = generatedInsuranceId.getInt(1);
            }

            //Here it returns an insurance object which contains insurance details
            InsuranceInfo insuranceObject = insuranceMap.get(insuranceIdInHashMap); 
            insuranceObject.setInsuranceId(newInsuranceId);
            //Here I am trying to store the newly inserted insurance object into main hashmap
            storeInsuranceInfoToHashMap(patientId, insuranceObject);

            //I am removing previous insurance object with temporary insurance id
            getInsuranceMap().get(patientId).remove(insuranceIdInHashMap);

            // Adding newly generated insurance id to array list
            getInsuranceIdsListInDatabase().add(newInsuranceId);
        }
    }

Well, nothing strange.嗯,没什么奇怪的。 You trying to add a new item into the Map object while you're iterating through it.您在遍历Map对象时尝试将其添加到Map对象中。

My advise would be trying to save the insurance data you created when iterating through the Map into a temporary Map object.我的建议是尝试将遍历Map时创建的保险数据保存到临时Map对象中。 And, after you finished iterating the Map .并且,在您完成迭代Map Then, you could save the temporary map into the initial map.然后,您可以将临时地图保存到初始地图中。

example :例子 :

public void storeInsuranceInformationToDataBase(int patientId) throws SQLException
{
    //It gives arrayList of isuranceIds which are already in the data base.
    ArrayList<Integer> insuranceIdsListInDatabase = getInsuranceIdsListInDatabase();  

    //It returns HashMap of insurance ids for given patient id. 
    HashMap<Integer, InsuranceInfo> insuranceMap = getInsuranceDetails(patientId);   
    Map<Integer, InsuranceInfo> tempInsuranceMap = new HashMap<>();
    Set<Integer> insuranceIdsInHashMap = insuranceMap.keySet();

    //Here I am getting ConcurrentModificationException
    for (int insuranceIdInHashMap : insuranceIdsInHashMap)
    {
        //Here I am comparing the both ids..
        if (insuranceIdsListInDatabase.contains(insuranceIdInHashMap))  
        {
            String updateInsuranceQuery = "UPDATE jp_InsuranceInfo SET  typeOfPolicy='" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "', amount=" + insuranceMap.get(insuranceIdInHashMap).getAmount() + ", duration=" + insuranceMap.get(insuranceIdInHashMap).getDuration()
                    + ", companyName='" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "' WHERE insuranceId=" + insuranceIdInHashMap + ";";

            getDataBase().getStatement().executeUpdate(updateInsuranceQuery);
        }
        else
        {
            String insertInsuranceQuery = "INSERT INTO jp_InsuranceInfo VALUES(" + insuranceMap.get(insuranceIdInHashMap).getPatientId() + ",'" + insuranceMap.get(insuranceIdInHashMap).getTypeOfPolicy() + "'," + insuranceMap.get(insuranceIdInHashMap).getAmount() + ","
                    + insuranceMap.get(insuranceIdInHashMap).getDuration() + ",'" + insuranceMap.get(insuranceIdInHashMap).getCompanyName() + "')";

            getDataBase().getStatement().executeUpdate(insertInsuranceQuery);

            ResultSet generatedInsuranceId = getDataBase().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
            int newInsuranceId = 0;
            if (generatedInsuranceId.next())
            {
                newInsuranceId = generatedInsuranceId.getInt(1);
            }

            //Here it returns an insurance object which contains insurance details
            InsuranceInfo insuranceObject = insuranceMap.get(insuranceIdInHashMap); 
            insuranceObject.setInsuranceId(newInsuranceId);
            //Here I am trying to store the newly inserted insurance object into main hashmap
            storeInsuranceInfoToHashMap(patientId, insuranceObject); <-- make this function to save those information into the temporary map

            //I am removing previous insurance object with temporary insurance id
            getInsuranceMap().get(patientId).remove(insuranceIdInHashMap);

            // Adding newly generated insurance id to array list
            getInsuranceIdsListInDatabase().add(newInsuranceId);
        }
    }
    insuranceMap.putAll(tempInsuranceMap);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM