简体   繁体   English

Java 生成 setter 查询名称

[英]Java generate setter query names

I am building an entity and i need to populate all the fields from a result set from a jpql resultset like like so:我正在构建一个实体,我需要从 jpql 结果集中的结果集中填充所有字段,如下所示:

    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.问题是有 50x 个周期,它看起来很丑陋并且占用大量空间。

is it possible to do it as a for loop something like this:是否可以将其作为 for 循环执行,如下所示:

for(int i=1; i<54; i++){
summary.setPeriod_[i]((BigDecimal)object[i+4]);
}

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) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM