如何在 JPA 中设置默认布尔值

问题描述 投票:0回答:12

我有一个属性

private boolean include;

我想将它的默认值设置为true,这样在数据库中它必须默认显示True。这在 JPA 中可能吗?

java jpa
12个回答
76
投票

据我所知,没有提供默认值的 JPA 本机解决方案。 这是我的解决方法:

非数据库便携解决方案

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

面向Java的解决方案

private boolean include = true;

面向Java的加Builder模式

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

这是我最喜欢的,而且不那么打扰。基本上它将定义默认值的任务委托给实体中的构建器模式。


12
投票

使用 JPA 2.1 和 Oracle 11 这对我有用,使用大小为 1 的 Oracle 类型 NUMBER:

爪哇:

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

创建 SQL 脚本:

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

11
投票

对于PostgreSQL你可以在定义中使用布尔值

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

5
投票

也许对使用 Microsoft SQL SERVER 的人有用

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

可能的值:0 或 1


3
投票

就我而言,对于 spring boot jpa,实体类中的以下语法有效。

@Builder.Default
private boolean columnName = false;

or

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

@Builder.Default 是为了确保我们在为此模型构造对象时具有默认值,仅当实体模型类使用@Builder 注释时才需要。否则,即使没有@Builder.Default,事情也能按预期正常工作。


1
投票

我发现在构造函数中添加是一个很好的解决方法,可以将新实体默认为一个值:

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

0
投票

您始终可以在方法上使用注释

@PreUpdate
@PrePersist
,您将设置在更新之前或保存到数据库之前应该做什么。

或者只是简单地设置值

private boolean include = true;


0
投票

如果您在数据库中定义了默认值,则可以选择列注释,并使用

insertable = false
作为参数,这样在插入值时它将选择您在数据库中默认标记的值。例子: 在 MySQL 中,我有一个 person 表,其状态属性为布尔类型,默认值为 true。 在您的 java 类中,它看起来像这样:

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

有关列注释的更多信息HERE,解释得很好,对我帮助很大。


0
投票

设置默认列值的简单方法是将其直接设置为实体属性值:

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

0
投票

如果你用的是MYSQL,我可以节省你5-6个小时的时间,你在寻找答案时会浪费掉这些时间。我尝试了多种方法,但对我来说,在 pom.xml 文件中更新了 spring boot data jpa 的 maven 依赖版本

<!-- 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>

0
投票

在使用 Lambok 构建器的情况下,不要忘记 @Builder.Default 注释:

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

-2
投票
private boolean include = true;
© www.soinside.com 2019 - 2024. All rights reserved.