Play Framework 2 Ebean indicates default value for field

I have a simple model in Play Framework 2, and I would like to specify the default value that needs to be inserted into the INT column if none of them are specified when executing INSERT.

Model:

@Entity @Table(name = "DashboardOptions", schema = "dbo") public class DashboardOptions extends Model implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id") public Long id; @Basic(optional = false) @Column(name = "userId") public Long userId; @Column(name = "chartType") public String chartType; public String name; public Integer size = 2; 

I would like the size column to be populated with 2 by default, however, if I specify a default value as above, my database evolution does not reflect this:

 create table dbo.DashboardOptions ( id numeric(19) identity(1,1) not null, userId numeric(19) not null, chartType varchar(255), name varchar(255), size integer, constraint pk_DashboardOptions primary key (id)) ; 

I would expect to see the following:

 create table dbo.DashboardOptions ( id numeric(19) identity(1,1) not null, userId numeric(19) not null, chartType varchar(255), name varchar(255), size integer default 2, constraint pk_DashboardOptions primary key (id)) ; 
+7
java ebean playframework-evolutions
source share
2 answers

Use your own columnDefinition as follows:

 @Column(columnDefinition = "integer default 2") public Integer size = 2; 
+12
source share

Another option is to use the @PrePersist javax.persistence tag package. you can have a bean decorated method with @PrePersist , and this method is called before calling Ebean.save. therefore, in this case, the following code will set the default value for the size to 2.

 @PrePersist protected void onCreate { if (this.size == null) this.size = 2; } 

This approach is applicable only in the context of ORM (Ebean) and, obviously, will not work directly with SQL. The advantage of this method is that it is more database neutral in the sense that integer default 2 cannot be a valid column definition string in some unknown strange RDBMS systems.

0
source share

All Articles