I have an existing JPA (EclipseLink) project where the desired behaviour is that if given a null value in a field for an entity, that null value should not be persisted.
The use case is that we may get several partial updates to these entities from external sources. Those sources may give us a null value, that does not mean "nullify this field" it means "I don't have this value".
Is there an annotation, pattern, or other tool that can be used to automate a null check in the setter OR tell JPA to not persist null values????
I can go through EVERY setter in EVERY entity and add if(val != null) { //set the value }
but that is tedious and repetitive.
For example we have:
@Entity
@Table(name = "my_table")
public class MyObject {
@Column
private String myColumn;
public String getMyColumn() {
return this.myColumn;
}
public void setMyColumn(String val) {
this.myColumn = val;
}
}
I would like to have something that would automatically help like this:
@Entity
@Table(name = "my_table")
public class MyObject {
@Column
@DontPersistIfNull
private String myColumn;
public String getMyColumn() {
return this.myColumn;
}
public void setMyColumn(String val) {
this.myColumn = val;
}
}
OR this:
@Entity
@Table(name = "my_table")
public class MyObject {
@Column
private String myColumn;
public String getMyColumn() {
return this.myColumn;
}
public void setMyColumn(String val) {
//AUTOGENERATED NULL CHECK
if(val != null) {
this.myColumn = val;
}
}
}