Hibernate / Hibernate 7 Basics Interview Questions
What is the Criteria API in Hibernate 7 and what are SelectionSpecification and RestrictionSpecification?
Hibernate 7 introduces a new type-safe Criteria-like API: SelectionSpecification and RestrictionSpecification - simpler than the JPA Criteria API for common cases while remaining fully type-safe via the static metamodel.
// Static metamodel auto-generated by hibernate-processor: // public abstract class Product_ { // public static volatile SingularAttribute<Product, Long> id; // public static volatile SingularAttribute<Product, String> name; // public static volatile SingularAttribute<Product, BigDecimal> price; // } // Hibernate 7: NEW SelectionSpecification API var results = SelectionSpecification .create(Product.class) .restrict(Restriction.equal(Product_.status, ProductStatus.ACTIVE)) .restrict(Restriction.contains(Product_.name, "book", false)) .restrict(Restriction.greaterThan(Product_.price, new BigDecimal("10"))) .sort(Order.desc(Product_.price)) .createQuery(session) .setMaxResults(20) .getResultList(); // RestrictionSpecification: add restrictions to an EXISTING query var priceFilter = RestrictionSpecification .of(Product.class) .where(Restriction.between(Product_.price, new BigDecimal("10"), new BigDecimal("100"))); var update = session.createMutationQuery("UPDATE Product SET discount = :disc"); priceFilter.applyTo(update); update.setParameter("disc", new BigDecimal("5")).executeUpdate(); // Traditional JPA Criteria still works: CriteriaBuilder cb = session.getCriteriaBuilder(); CriteriaQuery<Product> cq = cb.createQuery(Product.class); Root<Product> root = cq.from(Product.class); cq.where(cb.equal(root.get(Product_.status), ProductStatus.ACTIVE)); List<Product> r = session.createQuery(cq).getResultList();
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...
