spring data JPA

How to set default column value in JPA

Reading Time: < 1 minute

There might be a situation when you want to set the default value of column in spring boot. You might be thinking how to set it even when you have set the default value in the database but you still get a null value stored in the database . It is because JPA includes the columns with null value in the insert query which you might not want to include since you want the default value to be stored in DB

To approach this problem

Define your @Column annotation like that:
@​Column(name = “IS_ACTIVE”, columnDefinition = “boolean default true”)
Use columnDefinition to define default value

If you choose this option, you have to use dynamic-insert, so Hibernate doesn’t include columns with null values

import org.hibernate.annotations.DynamicInsert;

@Table(name = "test_config")
public class TestConfig implements Serializable {

  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(columnDefinition = "SERIAL", name = "id")
  private Long id;

  @Column(name = "name")
  private String name;

  @Column(name = "weight")
  private float weight;

  @Column(name = "is_active" ,columnDefinition = "boolean default true")
  private Boolean isActive;


If you want to do using Java

You can set the default value in the request body

public class TestConfigData {
  private String name;
  private float weight = 1;
  private Int age = 25;	
  private boolean isActive = true;

References :


Leave a Reply