Spring CrudRepository存储更新会覆盖其他字段

时间:2018-05-29 08:41:34

标签: java spring spring-boot spring-data

我在项目中使用CrudRepository进行数据库操作。当我使用save方法更新持久化数据时,由Spring实现,其他字段正在被覆盖。例如,我只发送firstName进行更新,但lastName正在转换为空字段。

简单地说,我用这样的实体调用save方法:

memberRepository.save(memberForUpdate);

我将此JSON发送给更新方法:

{"id":2,"firstName": "Rubens","lastName": "Barichello"}

Spring将此JSON转换为其余api端点的Member。 Member.java是:

package com.ilkaygunel.entities;

import java.time.LocalDateTime;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@Entity
@JsonInclude(Include.NON_NULL)
public class Member {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;
    private String email;
    private boolean enabled;
    private String password;
    private String memberLanguageCode;
    private String activationToken;
    private LocalDateTime activationTokenExpDate;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private MemberRoles roleOfMember;

    @Override
    public String toString() {
        return String.format("Member [id=%d, firstName='%s', lastName='%s', email='%s']", id, firstName, lastName,
                email);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public MemberRoles getRoleOfMember() {
        return roleOfMember;
    }

    public void setRoleOfMember(MemberRoles roleOfMember) {
        this.roleOfMember = roleOfMember;
    }

    public String getActivationToken() {
        return activationToken;
    }

    public void setActivationToken(String activationToken) {
        this.activationToken = activationToken;
    }

    public LocalDateTime getActivationTokenExpDate() {
        return activationTokenExpDate;
    }

    public void setActivationTokenExpDate(LocalDateTime activationTokenExpDate) {
        this.activationTokenExpDate = activationTokenExpDate;
    }

    public String getMemberLanguageCode() {
        return memberLanguageCode;
    }

    public void setMemberLanguageCode(String memberLanguageCode) {
        this.memberLanguageCode = memberLanguageCode;
    }
}

如何阻止覆盖其他字段并仅提供仅更新已发送字段?

1 个答案:

答案 0 :(得分:1)

save方法本身的行为与saveorUpdate类似,因此问题在于您的代码。

您所做的只是设置一些值而不是其他值。因此,当您将部分json发送到spring时,它会将其他值设置为其默认值。现在问题出在这里,null是数据库中的有效值。因此,CrudRepository会更新所有值以及null值,因为它认为您希望使用对象中的值更新到数据库。请考虑您的方案:

Json被送到Spring:private long id=2; private String firstName="Rubens"; private String lastName="Barichello"; private String email=null; private boolean enabled=false; private String password=null; private String memberLanguageCode=null; private String activationToken=null; private LocalDateTime activationTokenExpDate=null;

Spring生成的对象:

memberfromspring;
memberforupdate= memberRepository.findById(memberfromspring.getId());
memberforupdate.setFirstName(memberfromspring.getFirstName());
memberforupdate.setLastName(memberfromspring.getLastName());
memberRepository.save(memberForUpdate);

所以你应该做的是用findById(ID id)方法获取对象,设置值然后调用save(S entity)方法。

例如

String lista;
EditText bevasarlolista = null;
bevasarlolista = view.FindViewById<EditText>(Resource.Id.bevasarlo);

if (items[position] == "Bevásárlólista")
{
    Button mentes = view.FindViewById<Button>(Resource.Id.hozzaad);
    bevasarlolista.Text = lista;

    mentes.Click += bevasarlolistaMentese;
}

private void bevasarlolistaMentese(object sender, EventArgs e)
{
    lista = bevasarlolista.Text;

    Toast.MakeText(Application.Context, bevasarlolista.Text, ToastLength.Long).Show();
}