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
More Related questions...