title: Add elements to an entity list in java or create it from native query hibernate
body: <p>I have a native hibernate query that returns a list of objects to me, with a <code>for</code> loop I would like to populate a class list, but I do not laugh because the list is always populated by the last element. Where's the error?</p>
  
  <p>This the code:</p>
  
  <pre><code>...
          List&lt;Object[]&gt; results = query.list();
  
          List&lt;PackDisTask&gt; packageDistTasks = new ArrayList&lt;PackDisTask&gt;();
          PackageDistributionTaskId taskId = new PackageDistributionTaskId();
          Object[] result = null;
          String r = "";
  
          for (int i = 0; i &lt; results.size(); i++) {
              PackageDistributionTask pdt = new PackageDistributionTask();
              result = results.get(i);
  
              if (result[0] != null) {
                  r = result[0].toString();
                  taskId.setFkPackageDistribution(Integer.parseInt(r));
              }
  
              if (result[10] != null) {
                  r = result[10].toString();
                  taskId.setFkTaskType(Integer.parseInt(r));
              }
  
              pdt.setId(taskId);
  
              packageDistTasks.add(pdt);
          }
  </code></pre>