如何将这个Java POJO在for循环中保存到Repository中?

huangapple go评论98阅读模式
英文:

How do I save this Java POJO in for loop into a Repository?

问题

  1. 我正在尝试循环创建 Java POJO但是 IntelliJ 出现了错误
  2. Company c1 = new Company("Company_Name_1");
  3. Company c2 = new Company("Company_Name_2");
  4. Company c3 = new Company("Company_Name_3");
  5. for (int i = 1; i < 4; i++) {
  6. try {
  7. companyRepository.save("c" + i); // 这里出错了!
  8. } catch (Exception ex) {
  9. System.out.println(ex.toString());
  10. }
  11. }
  12. 我还尝试过
  13. companyRepository.save("c" + String.valueOf(i)); // 出错了
  14. companyRepository.save("c" + Integer.getInteger(i)); // 出错了
  15. 错误显示类型参数 'S' 的推断类型 'S' 不在限定范围内
  16. 对于如何修复此错误有任何想法吗
英文:

I'm trying to loop over Java POJO creation but intellij is giving errors.

  1. Company c1 = new Company(&quot;Company_Name_1&quot;);
  2. Company c2 = new Company(&quot;Company_Name_2&quot;);
  3. Company c3 = new Company(&quot;Company_Name_3&quot;);
  4. for (int i = 1; i &lt; 4; i++ ) {
  5. try {
  6. companyRepository.save(&quot;c&quot; + i); // this!
  7. } catch ( Exception ex ) {
  8. System.out.println(ex.toString());
  9. }
  10. }

I've also tried

  1. companyRepository.save(&quot;c&quot; + String.valueOf(i)); // error
  2. companyRepository.save(&quot;c&quot; + Integer.getInteger(i)); // error

Error says Inferred Type 'S' for type parameter 'S' is not within bounds.

Any ideas on fixing this error?

答案1

得分: 1

  1. Arrays.asList(c1, c2, c3).forEach(c -&gt; {
  2. try {
  3. companyRepository.save(c);
  4. } catch (Exception ex) {
  5. System.out.println(ex.toString());
  6. }
  7. })
英文:
  1. Arrays.asList(c1, c2, c3).forEach(c -&gt; {
  2. try {
  3. companyRepository.save(c);
  4. } catch ( Exception ex ) {
  5. System.out.println(ex.toString());
  6. }
  7. })

huangapple
  • 本文由 发表于 2020年10月24日 10:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64509503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定