Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2 Ebean specify default value for field

I have a simple model in Play Framework 2, and I would like to specify a default value to be inserted on a specify INT column if none is provided when the INSERT is performed.

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'd like to have the size column populate with 2 by default, however, if I specify the 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))
;

What I would expect to see is 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 default 2,
constraint pk_DashboardOptions primary key (id))
;
like image 606
sean.boyer Avatar asked Feb 04 '14 15:02

sean.boyer


1 Answers

Use own columnDefinition like this:

@Column(columnDefinition = "integer default 2")
public Integer size = 2;
like image 146
biesior Avatar answered Nov 14 '22 00:11

biesior