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) // )
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
