O'Reilly logo

Mastering Hibernate by Ramin Rad

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required