How to set a default entity property value with Hibernate

Dejell picture Dejell · Jun 24, 2010 · Viewed 253.5k times · Source

How do I set a default value in Hibernate field?

Answer

Petar Minchev picture Petar Minchev · Jun 24, 2010

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.

But if you don't want database default value, but simply a default value in your Java code, just initialize your variable like that - private Integer myColumn = 100;