Hibernate / Hibernate 7 Basics Interview Questions
What is @SQLRestriction and @SQLJoinTableRestriction in Hibernate 7?
Hibernate 7 adds @SQLRestriction (replacing removed @Where) to apply a permanent SQL WHERE clause to an entity or collection mapping.
import org.hibernate.annotations.SQLRestriction; // Soft-delete: only ever load non-deleted records @Entity @SQLRestriction("deleted_at IS NULL") // applied to ALL queries public class Product { @Id private Long id; private String name; private LocalDateTime deletedAt; public void softDelete() { this.deletedAt = LocalDateTime.now(); } } // session.find(Product.class, id) -> only finds non-deleted products // 'FROM Product' HQL -> only returns non-deleted products // On a collection: @OneToMany(mappedBy="category") @SQLRestriction("deleted_at IS NULL") // only active products in collection private List<Product> activeProducts; // @SQLJoinTableRestriction: restrict on join table for @ManyToMany @ManyToMany @JoinTable(name="student_course") @SQLJoinTableRestriction("enrolled_at > CURRENT_DATE - INTERVAL '1 year'") private List<Course> recentCourses; // NOTE: @Where (old annotation) was REMOVED in Hibernate 7 // @Where(clause="deleted_at IS NULL") <- REMOVED // @SQLRestriction("deleted_at IS NULL") <- USE THIS
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...
