views:

8770

answers:

7

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

+3  A: 

This isn't possible in JPA.

Here's what you can do with the Column annotation: http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html

fiddlesticks
Not being able to specify a default value seems a serious shortcoming of the JPA annotations.
Derek Mahar
JDO allows it in its ORM definition, so JPA ought to include this ... one day
+2  A: 

You can't do this with the column annotation. I think the only way is th set the default value when a object is created. Maybe the default constructor would be the right place to do that.

Timo
+11  A: 

Actually it is possible in JPA, although a little bit of a hack using the columnDefinition property of the @Column annotation, for example:

@Column(name="Price", columnDefinition="Decimal(10,2) default '100.00'")

Cameron Pope
Decimal(10,2), what does 10 and 2 stand for?
Schildmeijer
10 is the Integer part, and 2 is the decimal part of the number so:1234567890.12 would be a supported number.
Nathan Feger
Note also, this columnDefinition is not necessarily database independent, and is certainly not automatically tied the the datatype in Java.
Nathan Feger
After creating an entity with a null field and persisting it, you wouldn't have the value set in it. Not a solution...
Pascal Thivent
+8  A: 

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.

Pablo
This is the right way to do it if you want your entities to have the correct value after insert. +1
Pascal Thivent
Setting the default value of a nullable attribute (one that has a non-primitive type) in the model class will break criteria queries that use an `Example` object as a prototype for the search. After setting a default value, a Hibernate example query will no longer ignore the associated column where previously it would ignore it because it was null. A better approach is to set all default values just before invoking a Hibernate `save()` or `update()`. This better mimics the behaviour of the database which sets default values when it saves a row.
Derek Mahar
test and confirm, this is the right way to set default value
Harry Pham
@Harry: This is the correct way to set default values except when you are using criteria queries that use an example as a prototype for the search.
Derek Mahar
A: 

@Column(columnDefinition="tinyint(1) default 1")

I just tested the issue. It works just fine. Thanks for the hint.


About the comments:

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

This one doesn't set the default column value in the database (of course).

asd
**It deosn't work fine** at the object level (you won't get the database default value after an insert in your entity). Default at the Java level.
Pascal Thivent
A: 

JPA doesn't support that and it would be useful if it did. Using columnDefinition is DB-specific and not acceptable in many cases. setting a default in the class is not enough when you retrieve a record having null values (which typically happens when you re-run old DBUnit tests). What I do is this:

public class MyObject { int attrib = 0;

/** Default is 0 */ @Column ( nullable = true ) public int getAttrib()

/** Falls to default = 0 when null */ public void setAttrib ( Integer attrib ) { this.attrib = attrib == null ? 0 : attrib; } }

Java auto-boxing helps a lot in that.

Marco
A: 

Neither JPA nor Hibernate annotations support the notion of a default column value. As a workaround to this limitation, set all default values just before you invoke a Hibernate save() or update() on the session. This closely as possible (short of Hibernate setting the default values) mimics the behaviour of the database which sets default values when it saves a row in a table.

Unlike setting the default values in the model class as this alternative answer suggests, this approach also ensures that criteria queries that use an Example object as a prototype for the search will continue to work as before. When you set the default value of a nullable attribute (one that has a non-primitive type) in a model class, a Hibernate query-by-example will no longer ignore the associated column where previously it would ignore it because it was null.

Derek Mahar