英文:
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 = "qwe-123,10\nrty-345,15\nasd-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 ?
答案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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论