Java 如何在 JPA 中设置默认布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28207359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set default boolean value in JPA
提问by Andre Coetzee
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?
我想将其默认值设置为 true,以便在数据库中它必须从默认值显示 True。这在 JPA 中可能吗?
采纳答案by Antonio Maria Sanchez Berrocal
As far as i known there is no JPA native solution to provide default values. Here it comes my workaround:
据我所知,没有提供默认值的 JPA 本机解决方案。这是我的解决方法:
Non database portable solution
非数据库便携解决方案
@Column(columnDefinition="tinyint(1) default 1")
private boolean include;
Java oriented solution
面向Java的解决方案
private boolean include = true;
Java oriented plus Builder pattern
面向 Java 的加构建器模式
@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.
这是我最喜欢的,不那么侵入性。基本上,它将定义默认值的任务委托给实体中的 Builder 模式。
回答by Milkmaid
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.
您始终可以使用注释@PreUpdate
或@PrePersist
方法,在其中设置更新之前或保存到数据库之前应执行的操作。
Or just simply setup the value private boolean include = true;
或者只是简单地设置值 private boolean include = true;
回答by Daniel Mora
Using JPA 2.1 and Oracle 11 this works for me by using Oracle type NUMBER of size 1:
使用 JPA 2.1 和 Oracle 11 这通过使用大小为 1 的 Oracle 类型 NUMBER 对我有用:
Java:
爪哇:
@Column(name = "ENABLED", nullable = false)
private boolean enabled = true;
Create SQL script:
创建 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")
);
回答by Alan Faz
I've found that adding in the constructor is a good workaround to default new entities to a value:
我发现在构造函数中添加是一个很好的解决方法,可以将新实体默认为一个值:
public EntityName(){
this.fieldToDefault = default;
}
回答by Mark Ma
private boolean include = true;
回答by Taras Halynskyi
Maybe will be useful for people who work with Microsoft SQL SERVER
也许对使用 Microsoft SQL SERVER 的人有用
@Column(columnDefinition="bit default 0")
private Boolean active;
Possible values: 0 or 1
可能的值:0 或 1
回答by efirat
For PostgreSQLyou can use boolean in definition
对于PostgreSQL,您可以在定义中使用 boolean
@Column(name = "isDeleted", columnDefinition = "boolean default true")
private boolean isDeleted = true;