Hibernate / Hibernate 7 Basics Interview Questions
What is the @Check constraint and @ColumnDefault annotation in Hibernate 7?
Hibernate 7 provides DDL-level annotations for database constraints and default values applied during schema generation.
@Entity @Check(name="price_positive", constraints="price > 0") // table-level check public class Product { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String name; @Column(nullable=false) @Check(name="stock_non_negative", constraints="stock >= 0") // column-level private int stock; @Column(precision=10, scale=2) private BigDecimal price; @ColumnDefault("'ACTIVE'") // DEFAULT 'ACTIVE' in DDL @Column(nullable=false) private String status; @ColumnDefault("CURRENT_TIMESTAMP") @Generated(EventType.INSERT) // re-read from DB after INSERT private LocalDateTime createdAt; } // Generated DDL: // CREATE TABLE product ( // id BIGINT NOT NULL, // stock INTEGER NOT NULL CHECK (stock >= 0), // price NUMERIC(10,2), // status VARCHAR(255) DEFAULT 'ACTIVE', // created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, // CONSTRAINT price_positive CHECK (price > 0) // )
More Related questions...