在春季启动中,如果在检查表中是否存在该数据对之后无法插入数据,那么如果该数据对不存在,则会发生插入

时间:2019-06-12 11:34:18

标签: spring-boot spring-data-jpa

我有一个表user_followers.user表中有两个字段user_id和following_id。我应该在检查后在此表中插入user_id,following_id,如果该对已经存在并且状态为非活动状态,那么我只需更改在我的代码中起作用的状态即可。但是,如果该对不存在,则将发生插入。

表user_followers:

CREATE TABLE `user_followers` (
  `id` BIGINT(255) NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT(100) NOT NULL,
  `following_id` BIGINT(100) NOT NULL,
  `status` ENUM('active','inactive') NOT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

控制器

@RequestMapping(value = "/add/follower", method = RequestMethod.POST)
    public JsonNode addFollower(HttpServletRequest httpServletRequest, @RequestBody String body) throws IOException {

        long startTime = System.currentTimeMillis();

        utility.printCallingUrl(httpServletRequest);

        JSONObject request = new JSONObject(body);
        JSONObject response = new JSONObject();

        String auth = httpServletRequest.getHeader("Authorization");
        String authentication = utility.getBasicAuth();

        if (auth.equals(authentication)) {
            try {
                UserFollowers userFollower = new UserFollowers();

                User user = userService.getUser(request.getLong("userId"));

                User following = userService.getUser(request.getLong("followingId"));

                userFollower = userFollowersService.getUserFollowerYYYY(user, following);

                if (userFollower == null) {
                    userFollower.setFollower(user);
                    userFollower.setFollowing(following);
                    userFollower.setStatus("active");
                    userFollower = userFollowersService.createUserFollowers(userFollower);

                    response = new JSONObject(utility.convertPOJOtoStringHiding(userFollower));

                } else if (userFollower != null && userFollower.getStatus().equals("inactive")) {
                    userFollower.setStatus("active");
                    userFollower = userFollowersService.updateUserFollower(userFollower);
                    response = new JSONObject(utility.convertPOJOtoStringHiding(userFollower));

                } else {
                    response = new JSONObject();
                }

            } catch (Exception e) {
                response = utility.createResponse(500, KeyWord.ERROR, e.toString());
                utility.showMessage(e.toString());
            }
        } else {
            response = utility.createResponse(500, KeyWord.ERROR, KeyWord.AUTHENTICATION);
        }

        return objectMapper.readTree(response.toString());
    }

服务部分

public UserFollowers createUserFollowers(UserFollowers userFollowers) {
        return userFollowersRepository.save(userFollowers);
    }

存储库部分

public UserFollowers getUserFollowerYYYY(User user, User following) {
        return userFollowersRepository.findByFollowerAndFollowing(user, following);
    }

如果控制器中的部分工作正常,则对数据进行测试。不确定为什么

if (userFollower == null) {
                    userFollower.setFollower(user);
                    userFollower.setFollowing(following);
                    userFollower.setStatus("active");
                    userFollower = userFollowersService.createUserFollowers(userFollower);

                    response = new JSONObject(utility.convertPOJOtoStringHiding(userFollower));

                }

这部分无效。

1 个答案:

答案 0 :(得分:1)

您需要初始化userFollower对象。在if条件下,您正在检查它是否为null,如果为null,则在其中设置值。但是您不能在null对象中设置值。下面的代码段必须适合您:

if (userFollower == null) {
                userFollower = new UserFollowers();
                userFollower.setFollower(user);
                userFollower.setFollowing(following);
                userFollower.setStatus("active");
                userFollower = userFollowersService.createUserFollowers(userFollower);

                response = new JSONObject(utility.convertPOJOtoStringHiding(userFollower));

            }