-4

I am trying to return a arraylist of POJO. When I debug, I see the the arraylist of pojo is 4 and I can see the entries but it returns only the last entry and prints it four times. Not sure where I am wrong.

Expected: aId - 123 aId - 234 aId - 456 aId - 678

Actual

aId - 678 aId - 678 aId - 678 aId - 678 UPDATED< initializing E inside the loop solved the problem

 @Override
        public List<E> findAllByLoginId(String loginId) {   
            String oracleUrl = oracleProperties.getUrl();
            List<E> el = new ArrayList<>();

            int page =0;            
            int totalPages = 1; 
            URL url;
            try {
                 for(page=0;page<totalPages;page++){
                     url = new URL(oracleUrl+loginId+"/page/" + page");
                     ObjectMapper mapper = new ObjectMapper();
                     UContent value = mapper.readValue(url, UContent.class);
                     List<UEntitiy> entities = value.getContent();
                     totalPages = value.getTotalPages(); 
                     total = value.getTotalElemenets();
                     if (entities!=null){
                      String query = null;          
                          for(UEntitiy item : entities){  
                              item = droolsHelper.createQueryWithDrools(item); //   Drools                   
                              if(!StringUtils.isEmpty(item.getCQuery()) && item.getCQuery()!=null){
                                  result = runNeo4j(item.getCQuery());// execute in neo4j
                                  if(result.list().size() == 0){
E e = new E();                                 
                                       e.setCId(cId);
                                       e.setR(r);
                                       e.setU(uId);                                
                                       el.add(e); 
                                   }                      
                             }              
                        }                     
                   }
                 }
                } 
                catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (JsonParseException e) {
                    e.printStackTrace();
                } catch (JsonMappingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return el;      

    }
WebDev
  • 250
  • 2
  • 5
  • 17
  • My guess is you are not creating a new instance of your object each time but just changing the properties of one instance over and over. – takendarkk Sep 26 '17 at 14:37
  • @WebDev Can you give us a sample of your JSON produced by your oracle URL ? – Menelaos Sep 26 '17 at 14:44

5 Answers5

4

It seems you should instantiate a new E within the actual loop. Otherwise you are using the same object and changing the value. Essentially you are adding the reference to the object on the list. So the way the code is now, you are adding the same object 4 times.

Put E e = new E(); in the same loop that you add to the list.

if(result.list().size() == 0){     e = new E();                                 
                                   e.setCId(cId);
                                   e.setR(r);
                                   e.setU(uId);                                
                                   el.add(e); 
                               }     
Menelaos
  • 23,508
  • 18
  • 90
  • 155
2

create your instance of E here

if(result.list().size() == 0){
    E e = new E();
    e.setCId(cId);
    e.setR(r);
    e.setU(uId);
    el.add(e);
}
2

You need to create the instance of E per iteration, but you created only one instance of E.

1

The problem here is that you create the instance of the class E only once at the beginning of your method and everytime you're still using the same object.
You should create the object e for each iteration:

if(result.list().size() == 0){    
     e = new E();                               
     e.setCId(cId);
     e.setR(r);
     e.setU(uId);                                
     el.add(e); 
}                      
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
1

You added 4 references to the same object: e Take E e = new E(); into the loop:

for(page=0;page<totalPages;page++){
 E e = new E();
...