Java 如何使用 Hibernate 设置默认实体属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3110266/
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 a default entity property value with Hibernate
提问by Dejell
How do I set a default value in Hibernate field?
如何在 Hibernate 字段中设置默认值?
采纳答案by Petar Minchev
If you want a real database default value, use columnDefinition
:
如果您想要一个真正的数据库默认值,请使用columnDefinition
:
@Column(name = "myColumn", nullable = false, columnDefinition = "int default 100")
Notice that the string in columnDefinition
is database dependent. Also if you choose this option, you have to use dynamic-insert
, so Hibernate
doesn't include columns with null
values on insert. Otherwise talking about default is irrelevant.
请注意,字符串 incolumnDefinition
是数据库相关的。此外,如果您选择此选项,则必须使用dynamic-insert
,因此在插入时Hibernate
不包括带有null
值的列。否则谈论默认是无关紧要的。
But if you don't want database default value, but simply a default value in your Java code, just initialize your variable like that - private Integer myColumn = 100;
但是,如果您不想要数据库默认值,而只是 Java 代码中的默认值,只需像这样初始化您的变量 - private Integer myColumn = 100;
回答by Tim Hoolihan
what about just setting a default value for the field?
只是为该字段设置默认值怎么样?
private String _foo = "default";
//property here
public String Foo
if they pass a value, then it will be overwritten, otherwise, you have a default.
如果他们传递了一个值,那么它将被覆盖,否则,你有一个默认值。
回答by Ian Dallas
One solution is to have your getter check to see if whatever value you are working with is null (or whatever its non-initialized state would be) and if it's equal to that, just return your default value:
一种解决方案是让您的 getter 检查您正在使用的任何值是否为空(或任何其未初始化状态),如果它等于该值,则返回您的默认值:
public String getStringValue(){
return (this.stringValue == null) ? "Default" : stringValue;
}
回答by CSA
If you want to do it in database:
如果要在数据库中执行此操作:
Set the default value in database (sql server sample):
在数据库中设置默认值(sql server 示例):
ALTER TABLE [TABLE_NAME] ADD CONSTRAINT [CONSTRAINT_NAME] DEFAULT (newid()) FOR [COLUMN_NAME]
Mapping hibernate file:
映射休眠文件:
<hibernate-mapping ....
...
<property name="fieldName" column="columnName" type="Guid" access="field" not-null="false" insert="false" update="false" />
...
See, the key is insert="false" update="false"
看,关键是 insert="false" update="false"
回答by dbricman
You can use @PrePersist
anotation and set the default value in pre-persist stage.
您可以使用@PrePersist
注释并在 pre-persist 阶段设置默认值。
Something like that:
类似的东西:
//... some code
private String myProperty;
//... some code
@PrePersist
public void prePersist() {
if(myProperty == null) //We set default value in case if the value is not set yet.
myProperty = "Default value";
}
// property methods
@Column(nullable = false) //restricting Null value on database level.
public String getMyProperty() {
return myProperty;
}
public void setMyProperty(String myProperty) {
this.myProperty= myProperty;
}
This method is not depend on database type/version underneath the Hibernate. Default value is set before persisting the mapping object.
此方法不依赖于 Hibernate 下的数据库类型/版本。在持久化映射对象之前设置默认值。
回答by user1310789
The above suggestion works, but only if the annotation is used on the getter method. If the annotations is used where the member is declared, nothing will happen.
上述建议有效,但前提是在 getter 方法上使用了注释。如果在声明成员的地方使用注释,则不会发生任何事情。
public String getStringValue(){
return (this.stringValue == null) ? "Default" : stringValue;
}
回答by Deepak Poojari
<property name="age" type="integer">
<column name="age" not-null="false" default="null" />
</property>
回答by Garvit Bansal
I searched for this and found many answers to default value for column.If you want to use default value defined in SQL Table then in @Column Annotation use "insertable = false". insertable
我搜索了这个并找到了许多列默认值的答案。如果你想使用 SQL 表中定义的默认值,那么在 @Column Annotation 中使用"insertable = false"。可插入
@Column(name = columnName, length = lengthOfColumn, insertable = false)
@Column(name = columnName, length = lengthOfColumn, insertable = false)
If you are using columnDefination it @Column annotation may be it won't work as it is Database dependent.
如果您使用 columnDefination,它 @Column 注释可能无法工作,因为它依赖于数据库。
回答by Bojan Petkovic
Working with Oracle, I was trying to insert a default value for an Enum
与 Oracle 合作,我试图为 Enum 插入一个默认值
I found the following to work the best.
我发现以下方法效果最好。
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private EnumType myProperty = EnumType.DEFAULT_VALUE;
回答by Bojan Petkovic
You can use the java class constructor to set the default values. For example:
您可以使用 java 类构造函数来设置默认值。例如:
public class Entity implements Serializable{
private Double field1
private Integer field2;
private T fieldN;
public Entity(){
this.field1=0.0;
this.field2=0;
...
this.fieldN= <your default value>
}
//Setters and Getters
...
}