首页 > 解决方案 > 如何更新实体中的单个数据?

问题描述

如何仅更新我的实体中的一个数据?

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

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

    private String name;
    private String surname;
    private String email;
    private String password;
    private String token; //THIS
}

我只想更新表中的令牌,如何使用存储库或其他方式来做到这一点?

标签: javaspringspring-boot

解决方案


默认情况下,如果id-property 是,存储库实现将考虑一个新实体null

您必须通过 id 获取现有数据userEntity并在此检索到的对象上设置令牌。然后使用保存此对象repo.save(userEntity)


推荐阅读