How to Set Default Value in JPA Entity

 We can set default value in JPA Entity using field initialization, @Column(columDefinition=” “), @ColumnDefault, @PrePersist and constructor. These are the ways to set a default value when a JPA Entity class is created, and then automatically certain value is assigned to the field of that class. In this topic, we will how to set default values in JPA Entity using these different ways.

Setting default values in JPA entity fields in Spring Boot

Different Ways to Set Default Value in JPA Entity

  1. Using Field Initialization
  2. Using @Column(columDefinition=” “) Annotation
  3. Using @ColumnDefault Annotation
  4. Using @PrePersist Annotation
  5. Using Constructor

Using Field Initialization

This is the simplest way to set a default value to the column is by initializing the field directly in the  JPA entity class.

Example

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Boolean active = true; // Default value set here
// Constructors, Getters, and Setters
}

How it works

The active field of the above JPA Entity class User is initialized with true, so new entities will have this value unless explicitly set otherwise.

Using @Column(columnDefinition=” “) Annotation

We can set a default value at the database level using the @Column annotation with columnDefinition attribute.

Example

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Column;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@Column(columnDefinition = "BOOLEAN DEFAULT true")
private Boolean active;
// Constructors, Getters, and Setters
}

How it works

Using this @Column(columnDefinition = “”), the database assigns true as the default value for the active column of the user table.

Note

This approach depends on the database and may not be portable across different database systems.

Using @ColumnDefault Annotation

This is the other way to set a default value to the column at the database level. If no value is provided during an insert operation then the database will use the specified default value. It is used when using Hibernate as a JPA provider.

Example

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Column;
import org.hibernate.annotations.ColumnDefault;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ColumnDefault("true")
private Boolean active;
// Constructors, Getters, and Setters
}

How it Works

The @ColumnDefault(“true”) ensures that when a new User entity is created without active, it will default to “true”.

Using @PrePersist Annotation

This annotation is used to set default values just before persisting an entity.

Example

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Boolean active;
@PrePersist
public void prePersist() {
if (active == null) {
active = true; // Setting default value if it's null
   }
}
// Constructors, Getters, and Setters
}

How it works

Before saving (persisting) the entity, the prePersist() method ensures that active is set to true if no value was provided.

Using Constructor

We can initialize default values in the entity’s constructor.

Example

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Boolean active;
public User() {
this.active = true; // Default value
 }
// Constructors, Getters, and Setters
}

How it works

Whenever a new User object is created, the constructor ensures active is set to true.

Comparison Table

ApproachWorks at Java LevelWorks at Database LevelHibernate-SpecificJPA StandardNotes
Field InitializationSimple but not enforced at the DB level
@Column(columnDefinition=” “)SQL-dependent, may need dialect adjustments
@ColumnDefaultHibernate-specific but useful
@PrePersistEnsures defaults before inserting data
ConstructorWorks at object creation but not in DB

When to Use?

If we need a Java-level default: Use Field Initialization or Constructor.
If we need a DB-level default: Use @Column(columnDefinition=””) or @ColumnDefault.
If we want to ensure a default before inserting: Use @PrePersist.
If using Hibernate and generating schema: @ColumnDefault is a good choice.

Conclusion

In Spring Boot JPA, we can set default values using Java code, JPA annotations, or database constraints. The best approach depends on the use case:

✔ Use Java field initialization (private boolean active = true;) for simple defaults.
✔ Use @PrePersist for dynamic values like timestamps.
✔ Use @Column(columnDefinition) for enforcing database-level defaults (but it’s not portable).
✔  Use @ColumnDefault for Hibernate and generating schema.

Leave a Comment