I have an entity model class called Customer that has the following field column that stores a hash of user password which is generated by PgCrypto in Postgresql:
@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;
}
}
Now I have a problem when saving any other fields through the repository of the Customer entity model class for the user it will save the password hash too generating a new password hash stored in the database. Is there any way to tell Spring to ignore a certain field when saving an entity model or is there any way to update column settings like changing "updatable" in "Column" annotation?