Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JPA中设置默认布尔值_Java_Jpa - Fatal编程技术网

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

Java 如何在JPA中设置默认布尔值,java,jpa,Java,Jpa,我有一个特点 private boolean include; 我想将其默认值设置为true,以便在数据库中它必须从默认值显示true。这在JPA中可能吗?您可以始终在方法上使用注释@PreUpdate或@PrePersist,在该方法中,您将设置更新之前或保存到数据库之前应该执行的操作 或者只需设置值private boolean include=true据我所知,没有JPA本机解决方案提供默认值。 下面是我的解决方法: 非数据库便携式解决方案 @Column(columnDefinitio

我有一个特点

private boolean include;

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

您可以始终在方法上使用注释
@PreUpdate
@PrePersist
,在该方法中,您将设置更新之前或保存到数据库之前应该执行的操作


或者只需设置值
private boolean include=true

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

非数据库便携式解决方案

@Column(columnDefinition="tinyint(1) default 1")
private boolean include;
private boolean include = true;
     @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;
      }
...
}
面向Java的解决方案

@Column(columnDefinition="tinyint(1) default 1")
private boolean include;
private boolean include = true;
     @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;
      }
...
}
面向Java的plus构建器模式

@Column(columnDefinition="tinyint(1) default 1")
private boolean include;
private boolean include = true;
     @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;
      }
...
}

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

使用JPA 2.1和Oracle 11这对我来说是可行的,使用大小为1的Oracle类型编号:

爪哇:

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

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

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

可能对使用Microsoft SQL SERVER的人员很有用

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

可能的值:0或1

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

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

如果在数据库中定义了默认值,则可以选择列注释,并使用
insertable=false
作为参数,这样在插入值时,它将选择数据库中默认标记的值。例子: 在MySQL中,我有一个带有布尔类型status属性的person表,默认值为true。 在java类中,它将如下所示:

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

您可以了解有关列注释的更多信息,它解释得很好,对我帮助很大。

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

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

您可以使用赋值来更改默认值。创建setter方法并设置true值。
private boolean include=true将在Java上工作。不过,您必须确保您的数据库直接支持
布尔值
字段,或者必须使用转换器?列(name=“Include”)私有布尔值Include=true;大家好,我的数据库是sql server,使用数据类型位,所以它应该支持布尔值。我在注释中尝试了上述方法,但defaultHi在数据库中仍然没有显示任何值。我尝试添加一些解释,为您的代码评级器提供上下文,而不仅仅是粘贴代码。虽然它对您来说可能非常有意义,但最好解释一下。您的值在读取过程中会被数据库值覆盖,因此这不是解决方案。另外
@Column(columnDefinition=“BOOLEAN DEFAULT false”)
适用于包括PostgreSQL在内的许多数据库。你知道JPA委员会建议开发人员做什么吗?如果我们在列定义中使用默认值,那么为什么要再次初始化为true?这是针对hibernate的。它直接考虑分配的值并将其插入表中