How to set a default entity property value with Hibernate

后端 未结 17 2209
一整个雨季
一整个雨季 2020-11-28 02:53

How do I set a default value in Hibernate field?

相关标签:
17条回答
  • 2020-11-28 03:16

    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.

    0 讨论(0)
  • 2020-11-28 03:17

    You can use the java class constructor to set the default values. For example:

    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
    ...
    
    }
    
    0 讨论(0)
  • 2020-11-28 03:20

    Working with Oracle, I was trying to insert a default value for an Enum

    I found the following to work the best.

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private EnumType myProperty = EnumType.DEFAULT_VALUE;
    
    0 讨论(0)
  • 2020-11-28 03:21
    <property name="age" type="integer">
    <column name="age" not-null="false" default="null" />
    </property>
    
    0 讨论(0)
  • 2020-11-28 03:22

    i'am working with hibernate 5 and postgres, and this worked form me.

    @Column(name = "ACCOUNT_TYPE", ***nullable***=false, columnDefinition="varchar2 default 'END_USER'")
    @Enumerated(EnumType.STRING)
    private AccountType accountType;
    
    0 讨论(0)
提交回复
热议问题