raju raju - 3 months ago 43
Java Question

Constraints in ebean java play framework not reflected in generated sql

I am using ebean ORM in my java based play framework and following is my model

package models;

import java.util.*;
import javax.persistence.*;

import com.avaje.ebean.annotation.CreatedTimestamp;
import com.avaje.ebean.annotation.UpdatedTimestamp;

import play.db.ebean.*;
import play.data.validation.*;

@Entity
@Table(name = "coupons")
public class Coupon extends com.avaje.ebean.Model {

private static final long serialVersionUID = 1L;

@Id
private Long id;

@Constraints.Required
@Constraints.MaxLength(80)
@Constraints.MinLength(10)
private String title;

@Constraints.Required
@Constraints.MaxLength(1000)
@Constraints.MinLength(10)
private String description;

@Column(name = "created_at")
@CreatedTimestamp
private Date createdAt;

@Column(name = "updated_at")
@UpdatedTimestamp
private Date updatedAt;

@Column(name = "valid_from")
private Date validFrom = new Date();

@Column(name = "valid_to")
private Date validTo = new Date((long)2147483647*1000);


}


Generated sql file is

create table coupons (
id bigserial not null,
title varchar(255),
description varchar(255),
valid_from timestamp,
valid_to timestamp,
created_at timestamp not null,
updated_at timestamp not null,
constraint pk_coupons primary key (id)
);

drop table if exists coupons cascade;


Does ebean convert the constraints into sql file? How do I make sure my constraints are reflected in sql file also?

Answer

Ebean does not read any of the Play constraint annotations.

@Constraints.Required
@Constraints.MaxLength(80)

So the Required and MaxLength are not read and hence don't effect the generated DDL.

Ebean reads the JPA annotations and the javax.validation.constraints annotations @NotNull and @Size. For example, you could use:

@NotNull
@Size(max = 80)
Comments