我想在春天在模型中设置列的默认值。
我知道我可以将它们设置为private boolean company=true;
设定栏的缺省值的另一种方法包括在columnDefinition
注释@Column
。
我试着这样做 @Column(nullable=false, columnDefinition="boolean default true") private boolean company;
但在我的表中,值company
设置为0
每当我运行此代码。我使用mysql工作台。
我做错了吗?还是有其他方法可以做到这一点?
编辑:我知道,在我的表布尔值不stored.Only 0或1将当我改变我columnDefinition
到@Column(nullable=false, columnDefinition="tinyint(1) default '1'")
然后也被存储到表中的值是0。如何在模型中使用注释设置列的默认值
1
A
回答
0
org.hibernate.annotations.Entity
是被stored.But淘汰,有方法,使dynamicUpdate
和dynamicInsert
或者您可以使用javax.persistence.PrePersist
注解,其之前执行的回调坚持
private Boolean active;
@PrePersist
public void before() {
if (active == null)
this.active = true;
}
0
数据库不会存储默认值为true或false 布尔型/布尔型。它将默认表示布尔或布尔数字格式(0/1)。如果需要,您还可以将其存储为'Y/N',如下所示:
@Type(type="yes_no")
public boolean getCompany();