How to set default boolean value in JPA

JavaJpa

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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")
);

Solution 3 - Java

For PostgreSQL you can use boolean in definition

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

Solution 4 - Java

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

Solution 5 - Java

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

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

Solution 6 - Java

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.

Solution 7 - Java

You can always use annotations @PreUpdate or @PrePersist on method where you will setup what should be done before update or before save into DB.

Or just simply setup the value private boolean include = true;

Solution 8 - Java

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.

Solution 9 - Java

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;
}

Solution 10 - Java

private boolean include = true;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndre CoetzeeView Question on Stackoverflow
Solution 1 - JavaAntonio Maria Sanchez BerrocalView Answer on Stackoverflow
Solution 2 - JavaDaniel MoraView Answer on Stackoverflow
Solution 3 - JavaefiratView Answer on Stackoverflow
Solution 4 - JavaTaras HalynskyiView Answer on Stackoverflow
Solution 5 - JavaAlan FazView Answer on Stackoverflow
Solution 6 - JavaSasi Kumar MView Answer on Stackoverflow
Solution 7 - JavaMilkmaidView Answer on Stackoverflow
Solution 8 - JavaDavids Gonzalez TigreroView Answer on Stackoverflow
Solution 9 - JavaOusamaView Answer on Stackoverflow
Solution 10 - JavaMark MaView Answer on Stackoverflow