Hibernate / Hibernate 7 Basics Interview Questions
What is HQL (Hibernate Query Language) in Hibernate 7?
HQL is Hibernate's object-oriented query language operating on entity objects rather than tables. Hibernate 7 adds new HQL functions including regexp, JSON/XML manipulation, and set-returning functions.
// Basic HQL - uses entity class name, not table name: List<Product> all = session .createQuery("FROM Product", Product.class) .getResultList(); // WHERE with named parameter: List<Product> active = session .createQuery("FROM Product WHERE status = :status", Product.class) .setParameter("status", ProductStatus.ACTIVE) .getResultList(); // Constructor expression (DTO projection): List<ProductSummary> s = session .createQuery( "SELECT new com.example.ProductSummary(p.id, p.name, p.price) " + "FROM Product p WHERE p.price < :max", ProductSummary.class) .setParameter("max", new BigDecimal("100")) .getResultList(); // JOIN on association fields: List<Order> orders = session .createQuery("FROM Order o JOIN o.customer c WHERE c.email = :email", Order.class) .setParameter("email", "alice@example.com") .getResultList(); // Pagination: List<Product> page = session .createQuery("FROM Product ORDER BY createdAt DESC", Product.class) .setFirstResult(0).setMaxResults(20).getResultList(); // Hibernate 7 new: regexp operator List<Product> matched = session .createQuery("FROM Product WHERE name regexp :pattern", Product.class) .setParameter("pattern", "^(Laptop|Phone)") .getResultList();
More Related questions...