Hibernate Check Annotation Hibernate Check Annotation sql sql

Hibernate Check Annotation


Yes it is possible if @Check is used at class level like this:

@Entity@Check(constraints = "COL_A IS NULL OR (COL_B IS NOT NULL and COL_C IS NOT NULL)")public class Sample {    @Column(name = "COL_A")    private Long a;    @Column(name = "COL_B")    private Long b;    @Column(name = "COL_C")    private Long c;}

(Note that I rewrote your condition using @jarlh comment.). The constraints clause of @Check annotation needs to refer to the name attribute of @Column (it must be pure SQL).

@Check annotation needs to be at class level because of a Hibernate bug.


Use next annotation at Class level to check all three columns, e.g.:

@Check(constraints = "COL_A >= 15 AND COL_B < 200 AND COL_C > 100")

being COL_A, COL_B and COL_C the column names to be checked

Do not check nullable columns. @NotNull is preferred over @Check.