Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare CHECK constraint with Spring JPA?

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.

like image 531
Dims Avatar asked Dec 22 '15 17:12

Dims


People also ask

How do you make a unique field in JPA?

A unique constraint can be either a column constraint or a table constraint. At the table level, we can define unique constraints across multiple columns. JPA allows us to define unique constraints in our code using @Column(unique=true) and @UniqueConstraint.

What is the use of @column annotation?

@Column annotation is used for Adding the column the name in the table of a particular MySQL database.

What is @table annotation in spring boot?

The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database. The @Table annotation provides four attributes, allowing you to override the name of the table, its catalog, and its schema, and enforce unique constraints on columns in the table.

What is @UniqueConstraint?

The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint.


1 Answers

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;
like image 143
Rafael Fontoura Avatar answered Oct 25 '22 02:10

Rafael Fontoura