2016-09-17 79 views
1

我想在春天在模型中设置列的默认值。
我知道我可以将它们设置为
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。如何在模型中使用注释设置列的默认值

回答

0
org.hibernate.annotations.Entity

是被stored.But淘汰,有方法,使dynamicUpdatedynamicInsert

或者您可以使用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();