开发者

Insert in DB for multiple records not working in webflux R2DBC

I am trying to insert some records in DB in one go, also tried for loop but it never happens, If I save a single record, it works

@RequiredArgsConstructor
@Service
public class UserServiceImpl  {

    private final UserRepository userRepo;

    private final FamilyRepository familyRepo;

    public Mono<ServerResponse> insertUserData(ServerRequest serverRequest) {
        return serverRequest.bodyToMono(UserAndFamilyRequest.class)
//                .map(userAndFamilyRequest -> {
//                    List<FamilyMember> list = userAndFamilyRequest.getFamilyMemberList();
//                    list.stream().forEach((familyMember) ->
//                            {
//                                System.out.println(familyMember.getName());
//                                FamilyMemberEntity familyMemberEntity =  new FamilyMemberEntity();
//                                familyMemberEntity.setAge(familyMember.getAge());
//                                familyMemberEntity.setName(familyMember.getName());
//                                familyRepo.save(familyMemberEntity);//doesn't work either
//                                try{
//                                    Thread.sleep(2000);
//                                }catch(Exception ex){
//
//                                }
//
//                            });
//                    return userAndFamilyRequest;
//                })
                .map(userAndFamilyRequest -> {
                    List<FamilyMember> list = userAndFamilyRequest.getFamilyMemberList();
                    var entityList = list.stream().map(familyMember -> {
                        FamilyMemberEntity familyMemberEntity = new FamilyMemberEntity();
                        familyMemberEntity.setName(familyMember.getName());
                        familyMemberEntity.setAge(familyMember.getAge());
                        return familyMemberEntity;
                    }).collect(Collectors.toList());
                    familyRepo.saveAll(entityList);//doesn't work
                    return userAndFamilyRequest;
                })
                .flatMap(userAndFamilyRequest -> {
                    UserEntity userEntity = new UserEntity();
                    User user = userAndFamilyRequest.getUser();
                    userEntity.setSeats(userAndFamilyRequest.getFamilyMemberList().size());
                    userEntity.setAge(user.getAge());
                    userEntity.setName(user.getName());
         开发者_如何学Go           return userRepo.save(userEntity);
                })
//                .flatMap(userAndFamilyRequest -> {
//                    FamilyMember familyMember = userAndFamilyRequest.getFamilyMemberList().get(0);
//                    FamilyMemberEntity familyMemberEntity =  new FamilyMemberEntity();
//                    familyMemberEntity.setAge(familyMember.getAge());
//                    familyMemberEntity.setName(familyMember.getName());
//                    return familyRepo.save(familyMemberEntity);//single save works
//                })
                .flatMap(userEntity -> ServerResponse.created(URI.create("users"+userEntity.getId()))
                        .contentType(MediaType.APPLICATION_JSON)
                        .bodyValue(userEntity));
    }


}

no error in the console

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜