Hibernate / Hibernate 7 Basics Interview Questions
What are named queries in Hibernate 7 and how are they type-safe with TypedQueryReference?
Named queries are pre-defined HQL/SQL queries. Hibernate 7 (Jakarta Persistence 3.2) adds TypedQueryReference for type-safe, compile-time references via the static metamodel.
@Entity @NamedQuery( name="Product.findByCategory", query="FROM Product p WHERE p.category = :category ORDER BY p.price ASC" ) public class Product { @Id private Long id; private String name; private String category; } // Traditional way (error-prone string reference): List<Product> p = session .createNamedQuery("Product.findByCategory", Product.class) .setParameter("category", "electronics") .getResultList(); // Typo in "Product.findByCategory" = runtime exception! // Jakarta Persistence 3.2 / Hibernate 7: // TypedQueryReference in static metamodel: // Product_: public static final TypedQueryReference<Product> QUERY_FIND_BY_CATEGORY; List<Product> p2 = session .createQuery(Product_.QUERY_FIND_BY_CATEGORY) // compile-time safe! .setParameter("category", "electronics") .getResultList(); // Renaming the named query = compile error, not runtime exception!
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...
