-2

I am building an entity and i need to populate all the fields from a result set from a jpql resultset like like so:

    summary.setPeriod_1((BigDecimal)object[4]);
    summary.setPeriod_2((BigDecimal)object[5]);
    summary.setPeriod_3((BigDecimal)object[6]);
    summary.setPeriod_4((BigDecimal)object[7]);

The problem is that there are 50x periods and it looks ugly and takes up lots of space.

is it possible to do it as a for loop something like this:

for(int i=1; i<54; i++){
summary.setPeriod_[i]((BigDecimal)object[i+4]);
}
Daryn
  • 1,551
  • 1
  • 15
  • 21

1 Answers1

1

You should try something a bit like this.

java.lang.reflect.Method method;
try {
  for(int i=1; i<54; i++){
       method = summary.getClass().getMethod("setPeriod_"+i, BigDecimal.class);
       method.invoke(summary, object[i]);
  }
} catch (SecurityException e) { ... }
  catch (NoSuchMethodException e) { ... }
  catch (IllegalArgumentException e) { ... }
  catch (IllegalAccessException e) { ... }
  catch (InvocationTargetException e) { ... }
Djory Krache
  • 347
  • 4
  • 9