Could not commit JPA transaction: Transaction marked as rollbackOnly

前端 未结 5 1618
不知归路
不知归路 2020-11-30 21:45

I\'m using Spring and Hibernate in one of the applications that I\'m working on and I\'ve got a problem with handling of transactions.

I\'ve got a service class that

5条回答
  •  我在风中等你
    2020-11-30 22:27

    Save sub object first and then call final repository save method.

    @PostMapping("/save")
        public String save(@ModelAttribute("shortcode") @Valid Shortcode shortcode, BindingResult result) {
            Shortcode existingShortcode = shortcodeService.findByShortcode(shortcode.getShortcode());
            if (existingShortcode != null) {
                result.rejectValue(shortcode.getShortcode(), "This shortode is already created.");
            }
            if (result.hasErrors()) {
                return "redirect:/shortcode/create";
            }
            **shortcode.setUser(userService.findByUsername(shortcode.getUser().getUsername()));**
            shortcodeService.save(shortcode);
            return "redirect:/shortcode/create?success";
        }
    

提交回复
热议问题