What's the recommended way to support default values in the database? If I have a table defined as (in PostgreSQL 9.3):
create table bw_queries (
id bigserial primary key,
project_name text not null,
query_name text not null,
created_at timestamp with time zone not null default current_timestamp,
updated_at timestamp with time zone not null default current_timestamp,
constraint bw_queries_name_idx unique (project_name, query_name)
and an Ebean model class as:
@Entity
@Table(name = "bw_queries")
public class BWQuery extends Model {
public static Model.Finder<Long, BWQuery> find = new Model.Finder<>(Long.class, BWQuery.class);
// this seems to be needed or it Ebean uses the wrong sequence name
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bw_queries_id_seq")
@Id public Long id;
@Constraints.Required public String projectName;
@Constraints.Required public String queryName;
public LocalDateTime createdAt;
public LocalDateTime updatedAt;
}
Then creating a new model instance fails when I try to save it as it attempts to explicitly store nulls into the two timestamps.
I've tried marking it @Basic(optional=true) and also marking it as a generated value with type AUTO to no avail. Is the correct way to just set the default values in the constructor/field (I've tried this and it works)? I could live with this but it seems like needless duplication of code if they are already defined in the DB.
Ian.