Hibernate / Hibernate 7 Basics Interview Questions
What is JPQL vs HQL in Hibernate 7?
JPQL is the standardised query language from Jakarta Persistence. HQL is a strict superset with additional Hibernate-specific features.
| Aspect | JPQL | HQL |
|---|---|---|
| Specification | Jakarta Persistence 3.2 standard | Hibernate superset of JPQL |
| Portability | Works with any JPA provider | Hibernate-specific |
| Functions | Standard functions | All JPQL + regexp, ilike, array, JSON, XML (Hibernate 7) |
| Set operations | UNION/INTERSECT/EXCEPT (JP 3.2) | Same as JPQL + more |
// JPQL (standard): EntityManager em = emf.createEntityManager(); List<Product> p = em .createQuery("SELECT p FROM Product p WHERE p.price < :max", Product.class) .setParameter("max", new BigDecimal("100")) .getResultList(); // HQL shorthand (Hibernate-specific): List<Product> p2 = session .createQuery("FROM Product WHERE price < :max", Product.class) .setParameter("max", new BigDecimal("100")) .getResultList(); // HQL-only: regexp List<Product> re = session .createQuery("FROM Product WHERE name regexp :pat", Product.class) .setParameter("pat", "^(Laptop|Phone)").getResultList(); // UNION (supported in both JPQL 3.2 and HQL): List<Object[]> combined = session.createQuery( "SELECT p.id, p.name FROM Product p WHERE p.category = :cat " + "UNION " + "SELECT s.id, s.name FROM Service s WHERE s.category = :cat", Object[].class).setParameter("cat", "tech").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...
