How do I set a default value in Hibernate field?
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.
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
...
}
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;
<property name="age" type="integer">
<column name="age" not-null="false" default="null" />
</property>
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;