SQL/DDL modifier

In this section, we will discuss annotations that modify the SQL or the DDL used by Hibernate to perform a query or generate schema objects. The DDL modifying annotations are important if you use Hibernate to generate your tables.

@Check

Using the @Check Hibernate annotation, you can add the CHECK constraints to your table. The constraint defined in the @Check annotation uses the syntax that is supported by the database of your choice.

The following listing adds a constraint to the price column:

@Entity
@Check(constraints = "price >= 0")
public class Item {
  @Id
  @GeneratedValue
  private long id;
  private String description;
  private double price;
  // setters and getters
}

The DDL generated using PostgreSQL, is shown here:

 create table Item ...

Get Mastering Hibernate now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.