(Using Spring)How to stop DTO from repeating when the map appears to be fine?

101 Views Asked by At

In this program I have three numbers the user enters in and I am trying to get the second number in the set to display by being pulled from the DTO so that I can confirm that everything is working fine. But something is going wrong as you'll see by the output...

User Enters: 858508321,858509491,858510385

//This code is what is being executed. (Think of it as main)
 private void handleSubmit(final AjaxRequestTarget target) {


        List<Long> msgNums = new ArrayList<Long>();

        msg_Num = Ta.getInput();

        String[] Numbers = msg_Num.split(",");

        for(String Number:Numbers){
             msgNums.add(Long.valueOf(Number));
        }

        System.out.println(msgNums.get(1));

        List<BulkReplayMessageDTO> brm = messageReplayDao.getMessageResults(msgNums);

        System.out.println(brm.get(1).getMsgNum());
 }

//This is the DAO
public class MessageReplayDao extends SimpleJdbcDaoSupport {


     private final static String sql = "SELECT MSG_NBR, MSG_CPSD_DATA"
                + " FROM nti_raw_msg"
                + " WHERE THRD_NAME IS NOT null AND THRD_NAME NOT LIKE"
                + " 'out%' AND MSG_NBR IN (:messageNumbers)";


    public List<BulkReplayMessageDTO> getMessageResults(final List<Long> msgNumList){
        SqlParameterSource parameters = new MapSqlParameterSource()
            .addValue("messageNumbers", msgNumList);
        List<BulkReplayMessageDTO> result = getSimpleJdbcTemplate().query(sql, new MessageReplayMap(), parameters);
        return result;
    }
}

//The Map
public class MessageReplayMap implements ParameterizedRowMapper<BulkReplayMessageDTO> {

    public MessageReplayMap(){
    }

    LobHandler lobHandler = new DefaultLobHandler();
    @Override
    public final BulkReplayMessageDTO mapRow(ResultSet rs, int rowNum)
            throws SQLException {
        final BulkReplayMessageDTO brm = new BulkReplayMessageDTO();
        System.out.println(rowNum);
        brm.setMsgNum(rs.getLong("MSG_NBR"));
        brm.setMSG(CompressionUtils.uncompress(lobHandler.getBlobAsBytes(rs, "MSG_CPSD_DATA")));

        return brm;
    }
}

//And finally the DTO
 public class BulkReplayMessageDTO{
      private static Long msgNum;
      private static String MSG;

      public  final Long getMsgNum() {
        return msgNum;
      }
      public final void setMsgNum(final Long msgNumTemp) {
        msgNum = msgNumTemp;
      }

      public  final String getMSG(){
          return MSG;
      }
      public final void setMSG(final String MSGTemp){
          MSG = MSGTemp;
      }
}

Notice that I have printed to the console in the handleSubmit method, and inside my map. The output I get is

858509491
0
1
2
858510385 

when it should be

858509491
0
1
2
858509491

I have no clue what the problem could be since I have found other code example that are pretty much the same and mine seems to be pretty similar. I am pretty new to using Spring, so sorry if the answer is really obvious.

1

There are 1 best solutions below

0
On BEST ANSWER

I have found the problem, and it was a really simple one. In the DTO the variables were made static from a previous problem I was working on. I had forgotten to get rid of it after I had fixed the issue. If I understand the meaning of static, then I had made all the instances of those variables the same. So that is why I could only print out the last number that had been entered in by the user.