Andre Coetzee
Andre Coetzee

Reputation: 1310

How to set default boolean value in JPA

I have an attribute

private boolean include;

I would like to set its default value to true, so that in the database it must display True from default. Is this possible in JPA?

Upvotes: 65

Views: 181609

Answers (11)

Listoun developer
Listoun developer

Reputation: 31

In case you are using Lombok builder then do not forget to add @Builder.Default annotation:

@Column(columnDefinition = "tinyint(1) default 0")
@Builder.Default
private boolean include = false;

Upvotes: 0

Taras Halynskyi
Taras Halynskyi

Reputation: 179

Maybe will be useful for people who work with Microsoft SQL SERVER

    @Column(columnDefinition="bit default 0")
    private Boolean active;

Possible values: 0 or 1

Upvotes: 6

Hitesh Yadav
Hitesh Yadav

Reputation: 1

If you are using MYSQL, I can save your 5-6 hours which you will waste if searching for the answer. I tried multiple ways but for me updating the maven dependency version of spring boot data jpa in pom.xml file

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.7.3</version>
</dependency>

Upvotes: 0

Sasi Kumar M
Sasi Kumar M

Reputation: 2630

In my case, for spring boot jpa, having the below syntax in entity class worked.

@Builder.Default
private boolean columnName = false;

or

@NotNull
@Builder.Default
@ColumnDefault("true")
private Boolean columnName = true;

@Builder.Default is to ensure we are having default values while constructing the object for this model, needed only when the entity model class is annotated with @Builder. If otherwise, even without having @Builder.Default, things are working fine as expected.

Upvotes: 4

Ousama
Ousama

Reputation: 2800

The easy way to set a default column value is to set it directly as an entity property value:

@Entity
public class Student {
    @Id
    private Long id;
    private String name = "Ousama";
    private Integer age = 30;
    private Boolean happy = false;
}

Upvotes: 0

If you have defined default values in your database, you can choose the column annotation, and as parameter you use insertable = false, in this way when inserting a value it will choose the one that you marked by default in the database. Example: In MySQL I have a person table with a status attribute of boolean type and it has by default the value true. In your java class it would look like this:

//....
public class Person implements Serializable {
    //.....
    @Column(insertable = false)
    private Boolean status;
    //...
}

You can have more information about the column annotation HERE, it is well explained and it helped me a lot.

Upvotes: 0

efirat
efirat

Reputation: 3867

For PostgreSQL you can use boolean in definition

@Column(name = "isDeleted", columnDefinition = "boolean default true")
private boolean isDeleted = true;

Upvotes: 18

Mark Ma
Mark Ma

Reputation: 3

private boolean include = true;

Upvotes: -3

Alan Faz
Alan Faz

Reputation: 140

I've found that adding in the constructor is a good workaround to default new entities to a value:

public EntityName(){
    this.fieldToDefault = default;
}

Upvotes: 1

Daniel Mora
Daniel Mora

Reputation: 2639

Using JPA 2.1 and Oracle 11 this works for me by using Oracle type NUMBER of size 1:

Java:

@Column(name = "ENABLED", nullable = false)
private boolean enabled = true;

Create SQL script:

CREATE TABLE "ACCOUNT"(
"ID" NUMBER(10,0) NOT NULL ENABLE,
"NAME" VARCHAR2(255 CHAR) NOT NULL ENABLE,
"PASSWORD" VARCHAR2(255) NOT NULL ENABLE,
"ENABLED" NUMBER(1,0) DEFAULT 1 NOT NULL ENABLE,
PRIMARY KEY ("ID")
);

Upvotes: 12

As far as i known there is no JPA native solution to provide default values. Here it comes my workaround:

Non database portable solution

@Column(columnDefinition="tinyint(1) default 1")
private boolean include;

Java oriented solution

private boolean include = true;

Java oriented plus Builder pattern

     @Column(nullable = false)
     private Boolean include;
     ...
     public static class Builder {
      private Boolean include = true; // Here it comes your default value
      public Builder include (Boolean include ) {
      this.include = include ;
      return this;
     }
     // Use the pattern builder whenever you need to persist a new entity.
     public MyEntity build() {
       MyEntity myEntity = new MyEntity ();
       myEntity .setinclude (include );
       return myEntity;
      }
...
}

This is my favorite and less intrusive. Basically it delegates the task to define the default value to the Builder pattern in your entity.

Upvotes: 78

Related Questions