Setting default values for columns in JPA

homaxto picture homaxto · Oct 13, 2008 · Viewed 417.1k times · Source

Is it possible to set a default value for columns in JPA, and if, how is it done using annotations?

Answer

Pablo Venturino picture Pablo Venturino · Jun 18, 2009

You can do the following:

@Column(name="price")
private double price = 0.0;

There! You've just used zero as the default value.

Note this will serve you if you're only accessing the database from this application. If other applications also use the database, then you should make this check from the database using Cameron's columnDefinition annotation attribute, or some other way.