如何将ArrayList从lambda表达式传递给JPA仓库方法?

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

How to pass ArrayList to JPA repository method from lambda expression?

问题

这里有一个实体类:

public class Call {
    private String uniqId;
    private String gwNumber;
}

字符串行:

String line = "qwe-123,10\nrty-345,15\nasd-567,17";

使用"\n"来分隔每个实体的数据:

Arrays.stream(result.split("\n"))
    .forEach(
        s -> {
            String[] callFields =  s.split(",");
            Call call = new Call();
            call.setUniqId(callFields[0]);
            call.setGwNum(callFields[1]);
            List<Call> callList = new ArrayList<>();
            callList.add(call);
        }
    );

如何将callList传递给JPA repository.saveAll(callList)方法?

英文:

There is entity class:

public class Call {
    private String uniqId;
    private String gwNumber;
}

String line = &quot;qwe-123,10\nrty-345,15\nasd-567,17&quot;;

Arrays.stream(result.split(&quot;\n&quot;)) // &quot;\n&quot; is used to separate data for each entity
    .forEach(
              s-&gt; {
                    String[] callFields =  s.split(&quot;,&quot;);  // &quot;,&quot; is used to separate data for each filed  of entity
                    Call call = new Call();
                    call.setUniqId(callFields[0]);
                    call.setGwNum(callFields[1]);
                    List&lt;Call&gt; callList = new ArrayList&lt;&gt;();
                    callList.add(call);
                   }
            );

How to pass callList to JPA repository.saveAll(callList) method ?

答案1

得分: 1

我认为你应该忘记从Stream中调用某物。我不是说这是不可能的,但这并不推荐。

class Call {}

List<Call> callList = Arrays.stream(result.split("\n"))
              .map(s -> {
                 Call call = new Call();
                 // ...
                 return call;
              })
              .collect(Collectors.toList());

List<Call> callList 可以写入数据库。

英文:

I think you should forget to call smth. from Steram. I do not say this is impossible, but it's not recommended.

class Call {}

List&lt;Call&gt; callList = Arrays.stream(result.split(&quot;\n&quot;))
          .map(s -&gt; {
             Call call = new Call();
             // ...
             return call;
          })
          .collect(Collectors.toList());

List&lt;Call&gt; callList could be written into DB.

huangapple
  • 本文由 发表于 2020年9月12日 00:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63850753.html
匿名

发表评论

匿名网友

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

确定