Doesn't work setting default value of property in Hibernate

前端 未结 2 952
北海茫月
北海茫月 2021-02-05 10:20

I have a boolean property in my entity. Here\'s my annotations for it:

@Column(name = \"IS_ACTIVE\", nullable = false, columnDefinition=\"BIT DEFAULT 1\", length         


        
相关标签:
2条回答
  • 2021-02-05 10:53

    Try using BOOLEAN data type, define your @Column annotation like that:

    @Column(name = "IS_ACTIVE", columnDefinition = "boolean default true", nullable = false)
    private Boolean active = true;
    
    0 讨论(0)
  • 2021-02-05 10:53

    reference to the correct answer in this link How to set default value in Hibernate

    If you want a real database default value, use 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.

    The key of the solution is dynamic-insert annotation. You can add it to your entity class as this example: @org.hibernate.annotations.Entity(dynamicInsert = true)

    0 讨论(0)
提交回复
热议问题