user339108
user339108

Reputation: 13101

Is there a way to specify the default value for a column in JPA

@Entity 
class Foo {

@Column
private String productName;

@column
private Date orderedDate;

@Column
private int numHits;
}

I would like to specify a default value of productName="Toy" and orderedDate="10 days before" and numHits as "5", so that these translate as 'default' values during hbm2ddl and reflect in the schema.

What annotation should I use, so that I can achieve this in a database agnostic fashion (e.g. for h2, postgres, mysql)

Upvotes: 0

Views: 423

Answers (2)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

So, if you want a default value for a given column, but you are not interested in the DDL details of how this should be guaranteed by the database, you might just as well simply initialize the field to its default value:

@Colum(name="emp_salary")
private double salary = 1500;

Wouldn't that work?

Upvotes: 2

DataNucleus
DataNucleus

Reputation: 15577

Not part of JPA explicitly, though you could include it into "columnDefinition" on @Column (which is obviously datastore-dependent, so a hack). You'd have to rely on vendor-specifics if you want datastore-agnosticity for that with JPA

JDO is the only persistence standard that explicitly allows that and hence is datastore agnostic

Upvotes: 1

Related Questions