6

How is it possible to know, which data annotations are supported by Spring JPA?

For example, is @Min supported?

   @Min(value = 0)
   private double calories;

Anyway, how can one declare CHECK constraint?

UPDATE

Please read about CHECK constraint. Also note, that @Min is a hyperlink.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Dims
  • 47,675
  • 117
  • 331
  • 600
  • "Spring JPA" does nothing other than provide a layer over the JPA API. JPA API provides nothing explicitly for adding a CHECK constraint in the datastore but allows you to specify columnDetails. Look at the javadocs –  Dec 22 '15 at 18:12
  • Thanks. But how to know that `javax.validation.constraints.Min` is not supported? – Dims Dec 22 '15 at 18:14
  • 4
    Apart from the question. This is not related to Spring JPA nor JPA itself. This is called Bean Validation (formerly known as JSR 303. Hibernate Validator is a reference implementation of JSR 303. It is JSR 349, since Bean Validation 1.1). – Tiny Dec 22 '15 at 18:15
  • 2
    So, please someone answer that no annotation for `CHECK` constraint exist and that the only way is `columnDetails` option. Also I would like downvotes removed, because question is absolutely normal. – Dims Dec 22 '15 at 18:19
  • You can always create a custom validation constraint of your own doing the same as `CHECK` in the underlying database. – Tiny Dec 22 '15 at 18:22
  • "is not supported" ? by what ? any jpa provider will enable a hook for such bean-validation annotations. –  Dec 22 '15 at 18:30
  • @Tiny so as with `UNIQUE` constraint. Nevertheless, an annotation exist for this. – Dims Dec 22 '15 at 18:30
  • It is very unlikely but I don't know as I am currently not actively concerned with Hibernate Validator. – Tiny Dec 22 '15 at 18:36

1 Answers1

10

About validations executed by javax.validation API I don't know. But you can declare a constraint using @javax.persistence.Column annotation. For example, to create a check constraint to force accept only some values to a VARCHAR column you coul use the following code:

@Column(columnDefinition = "VARCHAR(60) CHECK (status IN ('ACTIVE', 'PENDING', 'INACTIVE')))
private String status;
Rafael Fontoura
  • 326
  • 4
  • 12