如何在运行时动态更新实体字段列设置?

时间:2019-02-10 07:17:03

标签: java postgresql spring-boot jpa pgcrypto

我有一个称为Customer的实体模型类,该类具有以下字段列,用于存储由Postgresql中的PgCrypto生成的用户密码的哈希值:

@Entity(name = "customers")
@Table(name = "customers")
public class Customer implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -3265467376787297897L;

    @JsonIgnore
    @Id
    @GeneratedValue(generator = "user_id_generator")
    @SequenceGenerator(name = "user_id_generator", sequenceName = "user_id_sequence", initialValue = 1)
    private Long id;
    private String email;
    //@JsonIgnore
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @ColumnTransformer(write = "digest(?, 'sha512')", read = "encode(password, 'hex')")
    @Column(columnDefinition = "bytea", updatable = false)
    private String password;
    @ColumnTransformer(write = "pgp_sym_encrypt(?, current_setting('encrypt.key'), 'cipher-algo=aes128')", read = "pgp_sym_decrypt(phone, current_setting('encrypt.key'), 'cipher-algo=aes128')")
    @Column(columnDefinition = "bytea")
    private String phone;
    @ColumnTransformer(write = "pgp_sym_encrypt(?, current_setting('encrypt.key'), 'cipher-algo=aes128')", read = "pgp_sym_decrypt(address, current_setting('encrypt.key'), 'cipher-algo=aes128')")
    @Column(columnDefinition = "bytea")
    private String address;


    public Long getId() {
        return id;
    }

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

    public String getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

现在,当通过客户实体模型类的存储库为用户保存任何其他字段时,它会保存密码哈希,同时也会生成存储在数据库中的新密码哈希,这是一个问题。在保存实体模型时,有没有办法告诉Spring忽略某个字段,或者有没有办法更新列设置,例如更改“列”注释中的“可更新”?

0 个答案:

没有答案