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

Tags: ,



There is entity class:

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

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

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

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

Answer

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<Call> callList = Arrays.stream(result.split("n"))
          .map(s -> {
             Call call = new Call();
             // ...
             return call;
          })
          .collect(Collectors.toList());

List<Call> callList could be written into DB.



Source: stackoverflow