Is there a way to prevent the preservation of null values ​​when resolving to others?

I have an existing JPA project (EclipseLink) where the desired behavior is that if a value is set to zero in the field for the object, this zero value should not be saved.

A use case is that we can receive several partial updates of these objects from external sources. These sources can give us a null value, which does not mean "invalidate this field", it means "I do not have this value."

Is there an annotation, template, or other tool that can be used to automate zero checking in the installer OR specify a JPA to not preserve null values?

I can go through an EVERY setter in EVERY entity and add if(val != null) { //set the value } , but this is tedious and repeats.

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 that:


@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;
  }
 }
}
+5
source share
2 answers

The Hibernate validator (and any implementation javax.validation) has an annotation @NotNullthat prohibits the entity that will be preserved if the annotated property is null. I'm not sure what hibernate-validatorwill work with EclipseLink, but there should also be an implementation javax.validationfor EclipseLink.

But if you want to block the installation of zeros - well, then change the layer that sets the values, not the entities themselves.

+2
source

, , ,

+2

All Articles