【问题标题】:How to update record in springboot JpaRepository?如何更新springboot JpaRepository中的记录?
【发布时间】:2016-02-01 13:10:21
【问题描述】:

我的模特是

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name="name")
private String name;

@Column(name="email")
private String email;   

//getter & setter
}

我的控制器是

@RequestMapping(value = "user/userProfile", method = RequestMethod.POST)

public String saveUserProfile(@Valid UserProfile userProfile, @RequestParam("file") MultipartFile file,
        BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("userProfile", userProfile);
        return "userprofileform";
    }           
    userProfileService.saveUserProfile(userProfile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "redirect:/user/home";
}
@RequestMapping("/user/userProfile/new")
public String newUserProfile(Model model) {
       UserProfile profile = userProfileService.getUserProfileByUserId(userService.getLoggedInUser().getId());
    model.addAttribute("userProfile", profile);
    return "userprofile";
}

仓库是

@Transactional
public interface UserProfileRepository extends CrudRepository<UserProfile, Long>{

}

当我第一次创建 userProfile 时,它​​会成功创建,但是当我尝试更新它时,它会创建新条目。

但是在这个控制器中,当我使用 findone() 并复制所有信息时,它可以正常工作。

下面的相同保存方法替换为:

public String saveUserProfile(@Valid UserProfile userProfile, @RequestParam("file") MultipartFile file,
        BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("userProfile", userProfile);
        return "userprofileform";
    }           
    UserProfile tempProfile = userProfileService.getUserProfileByUserId(userService.getLoggedInUser().getId());
    try {                               
        if (tempProfile != null) {
            tempProfile.setName(userProfile.getName());              
            tempProfile.setEmail(userProfile.getEmail());                               userProfileService.saveUserProfile(tempProfile);
        } else {
            userProfile.setUserId(userService.getLoggedInUser().getId());
            userProfileService.saveUserProfile(userProfile);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "redirect:/user/home";
}

这是写入方式还是我们如何更新记录?

还是保存在会话属性中(@SessionAttributes("yourAttributeName"))只是写法?

【问题讨论】:

    标签: spring spring-mvc spring-boot spring-data-jpa


    【解决方案1】:

    确保为 userProfile 对象分配了一个 id。如果您的对象没有 id,hibernate 会假定它是一个新实体,因此它会在数据库中创建一条新记录。

    【讨论】:

    • 是的,我在控制器中设置了 ID,我使用登录用户 ID 调用了 findone,并获取了现有的配置文件对象,数据中的 setid 来自表单请求。我也使用隐藏字段。
    【解决方案2】:

    您需要确认当您想要进行更新时,UserProfile 有一个 id。将调试点放在:profileRepo.save(userProfile) 以确认这一点,因为如果不存在 id 将创建一个新实体,如果 id 已经存在则应该更新。

    【讨论】:

    • 我检查了这个,当表单提交时没有 ID,但在我的控制器中手动设置 id 然后保存记录。
    • 你能粘贴你手动设置的代码吗?当您尝试更新它时,您是将其设置为新值还是数据库中的现有值?另外,您将其设置为什么值以及何时保存新的 id 值?
    • 我也添加了代码。 UserProfile tempProfile = userProfileService.getUserProfileByUserId(userService.getLoggedInUser().getId()); userProfile.setId(tempProfile.getId());
    猜你喜欢
    • 2021-04-24
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多