Record a problem with setting default values using Spring Data JPA

  • 2021-12-13 08:14:32
  • OfStack

Question 1 of setting default value of directory Spring Data JPA began to pass through Baidu, and the writing method was changed as follows. Under the guidance of big brothers, there was a third writing method Jpa to set default value constraint 1, modify column definition attribute 2 when building table, and pass Hibernate (org. hibernate. annotations. ColumnDefault)

Spring Data JPA Problems Setting Default Values

I have an entity entity with a Boolean field in it:


//entity table A Brief Annotation 
public class TableEntity { 
    private Boolean b; 
 public Boolean getB() {
        return b;
    }    
    public void setB(Boolean b) {
        this.b= b;
    }
}

Then now you need to set the default value true for this Boolean variable

1 started to pass Baidu, and the writing is like this


//entity table A Brief Annotation 
public class TableEntity {
 @Column(name = "b", columnDefinition = "bit default 1", nullable = false)
    private Boolean b; 
 public Boolean getB() {
        return b;
    }    
    public void setB(Boolean b) {
        this.b= b;
    }
}

In fact, this writing should be no problem. At that time, the database was sql server, but there was a problem when changing the environment deployment and switching to MySQL. It was suspected that I wrote the problem here (in fact, I always felt that it should not matter)

So I changed the second edition


//entity table A Brief Annotation 
public class TableEntity {
 @Column(name = "b", nullable = false)
    @org.hibernate.annotations.Type(type = "yes_no")
    private Boolean b = true; 
 public Boolean getB() {
        return b;
    }    
    public void setB(Boolean b) {
        this.b= b;
    }
}

Assigning private attribute values directly is also a method mentioned in some articles after passing Baidu. As for the annotation of type, Boolean variables are stored in the database through character variables, and "Y" or "N" are stored.

However, this writing method still has problems after the project runs, and the default value cannot be saved, which is equivalent to writing in vain.

Under the guidance of the big brother, there is a third writing method


//entity table A Brief Annotation 
public class TableEntity {
 @Column(name = "b", nullable = false)
    @org.hibernate.annotations.Type(type = "yes_no")
    private Boolean b = true; 
 public Boolean getB() {
        if(b==null) {
            return true;
        }
        return b;
    }    
    public void setB(Boolean b) {
        if(b==null) {
            return;
        }
        this.b= b;
    }
}

It probably means that when JPA is saved, the framework will call get/set method to assign and take values, so it is OK to assign default values directly in get/set method.

The actual test results are selected.

Jpa Setting Default Value Constraints

Two Ways to Set Default Value Constraints for Fields Using SpringDataJpa

1. Modify the column definition attributes when building a table


@Column(columnDefinition="INT DEFAULT '1'")
private Integer status;

2. Through Hibernate (org.hibernate. annotations. ColumnDefault)

Set the default value with the comments provided under


@ColumnDefault("1")
private Integer status;

Related articles: