title: Why are multiple DTOs altering the session Map?
body: <p>In My recent project, I am using jQuery Grid, generating dynamic grids when user click on button. </p>
  
  <p>At server side data is manipulated by using the <code>Session</code>. </p>
  
  <p>First I am getting <code>Session</code> object and casting that object to <code>gatepassDTO</code>. </p>
  
  <p>Then I am creating one local object and I am copying properties from old to new object and putting into the <code>Map</code>. </p>
  
  <p>After that I am creating <code>futureDTO</code> object and copying properties from <code>gatepassDTO</code> to <code>futureDTO</code>. If I change anything in <code>futureDTO</code> it effects on the Map that contains object.</p>
  
  <p>Why this happens?</p>
  
  <pre><code>public String addOnemoreGatepass() {
  
          log.info("----------------Entering method addOnemoreGatepass------------");
          try {
              Object map = session.get("gatepassMap");
  
              GatepassDTO gatepassDTO = new GatepassDTO();
              gatepassDTO=(GatepassDTO)session.get("gatepassDTO");
              if(map!=null){
  
                  //gatepassMap=Collections.synchronizedMap((HashMap)map);
              }
              if(truckNo!=null){
                  gatepassDTO.setTruckNo(truckNo);
              }
  
                       if(gpDirect!=null){
                  GatepassDTO tempDTO=new GatepassDTO();
                  copyProperty(tempDTO,gatepassDTO);
              /*  HashMap&lt;String,GatepassDTO&gt; maps=null;
                  if(gatepassNo.equals("1")){
                      maps=new HashMap&lt;String, GatepassDTO&gt;();
                  local.saveMap(getRequest().getSession().getId(),maps);
                  }
                  maps=local.loadMap(getRequest().getSession().getId());
                  maps.put(gatepassNo, tempDTO);*/
                  putCachedObject("SINGLEGATEPASSCACHE", gatepassNo, tempDTO);
                  gatepassMap.put(gatepassNo, tempDTO);
  
                  //local.saveMap(getRequest().getSession().getId(),maps);
                  session.put("documentType", documentType);
                  session.put("gatepassMap", gatepassMap);
                  return SUCCESS;
              }
              else{
                  GatepassDTO tempDTO=new GatepassDTO();
                  copyProperty(tempDTO,gatepassDTO);
                  /*HashMap&lt;String,GatepassDTO&gt; maps=null;
                  if(gatepassNo.equals("1")){
                      maps=new HashMap&lt;String, GatepassDTO&gt;();
                  local.saveMap(getRequest().getSession().getId(),maps);
                  }
                  maps=local.loadMap(getRequest().getSession().getId());
                  maps.put(gatepassNo, tempDTO);*/
                  putCachedObject("SINGLEGATEPASSCACHE", gatepassNo, tempDTO);
                  gatepassMap.put(gatepassNo, tempDTO);
  
                  //local.saveMap(getRequest().getSession().getId(),maps);
                  session.put("documentType", documentType);
                  session.put("gatepassMap", gatepassMap);
  
              }
              GatepassDTO futureDTO=new GatepassDTO();
              copyProperty(futureDTO,gatepassDTO);
  
              DocumentDTO documentDTO =futureDTO.getDocumentDTO();
              List&lt;GatepassGoodsDTO&gt; goodsList=documentDTO.getGatepassGoodsList();
              int i=0;
              for(GatepassGoodsDTO goodsDTO:goodsList){
                  if(goodsDTO.getRemovalType()!=null&amp;&amp;(goodsDTO.getRemovalType().equals("Quantity")||goodsDTO.getRemovalType().equals("Weight"))){
                  goodsDTO.setPrevRemovalType(goodsDTO.getRemovalType());
                  goodsDTO.setPrevGPQuantity((goodsDTO.getRemovalQuantity()!=null?goodsDTO.getRemovalQuantity():0));
                  goodsDTO.setPrevGPWeight((goodsDTO.getRemovalWeight()!=null?goodsDTO.getRemovalWeight():0));
                  goodsDTO.setBalanceQuantity(goodsDTO.getBalanceQuantity()-goodsDTO.getRemovalQuantity());
                  goodsDTO.setBalanceWeight(goodsDTO.getBalanceWeight()-goodsDTO.getRemovalWeight());
                  goodsDTO.getVehicleDetailsList().clear();
                  goodsDTO.setVehicleDetailsList(goodsDTO.getDeletedVehicleDetailsList());
                  goodsDTO.setDeletedVehicleDetailsList(new ArrayList&lt;GatepassVehicleDTO&gt;());
                  goodsDTO.setRemovalType("");
                  goodsDTO.setRemovalQuantity(0);
                  goodsList.set(i, goodsDTO);}
                  else{
                      goodsList.set(i, goodsDTO);
                  }
                  i++;
              }
              documentDTO.setGatepassGoodsList(goodsList);
              documentDTO.setContainerList(deletedContainerModel);
              futureDTO.setDocumentDTO(documentDTO);
              futureDTO.setTruckModel(new ArrayList&lt;GatepassTruckDTO&gt;());
              session.put("gatepassDTO",futureDTO);
              // setDocumentModel(null);
              // setGridModel(null);
              deletedVehicleModel.clear();
              deletedContainerModel.clear();
              // manifestNo=null;
  
  
  
          } catch (Exception e) {
              log.info(e.getMessage());
          } 
          log.info("---------------Ending method addOnemoreGatepass----------------");
          return SUCCESS;
      }
  
  
  private void copyProperty(GatepassDTO tempDTO,GatepassDTO gatepassDTO){`enter code here`
          tempDTO.setDocumentDTO(gatepassDTO.getDocumentDTO());
          tempDTO.setTruckNo(gatepassDTO.getTruckNo());
      }
  </code></pre>
  
  <p>Why this problem occurs? 
  Is this core Java problem or Struts2 JSON problem?</p>