gpt4 book ai didi

java - 修改反射(reflect)在本地引用中的类级别 HashMap 更改

转载 作者:行者123 更新时间:2023-11-30 11:01:18 25 4
gpt4 key购买 nike

我正在尝试根据 insuranceid 将保险详细信息保存到数据库中,或者如果 insuranceid 已存在于数据库中则进行更新。问题是,当我尝试保存多个保险详细信息时,它给出了 ConcurrentModificationException。在调试时我发现第一个周期是可以的,即第一个保险详细信息已成功保存到数据库中。但是当存储下一个保险详细信息时,它会给出异常。另一件事是,我在这里修改主 HashMap(patientId,HashMap(insuranceId,InsuranceInfo)),并且更改反射(reflect)在本地引用中,即 id 正在添加到当前 method() 中的 insuranceMap 变量。请尝试帮助我

这是我的存储方式...

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);
}
}

最佳答案

嗯,没什么奇怪的。您在遍历 Map 对象时尝试将新项目添加到它。

我的建议是尝试将您在遍历 Map 时创建的保险数据保存到临时 Map 对象中。并且,在您完成迭代 Map 之后。然后,您可以将临时 map 保存到初始 map 中。

例子:

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);
}

关于java - 修改反射(reflect)在本地引用中的类级别 HashMap 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31240304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com