2020-04-24

Hibernate JPA @Check annotation Eample

@Check

The @Check annotation is used to specify an arbitrary SQL CHECK constraint which can be defined at the class level.

Check Arbitrary SQL CHECK constraints which can be defined at the class, property or collection level.

Database-level checks

Hibernate offers the @Check annotation so that you can specify an arbitrary SQL CHECK constraint which can be defined as follows:

Database check entity mapping example

@Entity(name = "Book")
@Check( constraints = "CASE WHEN isbn IS NOT NULL THEN LENGTH(isbn) = 13 ELSE true END")
public static class Book {

@Id
private Long id;

private String title;

@NaturalId
private String isbn;

private Double price;

//Getters and setters omitted for brevity

}

No comments:

Post a Comment