Hibernate / EclipseLink Interview questions
What is the difference between JPQL and the EclipseLink native Expression/Criteria API?
JPQL is a string-based query language, parsed at runtime (or precompiled for named queries), and works well for queries known ahead of time. The JPA Criteria API and EclipseLink's own native Expression framework instead build queries programmatically as Java objects, which suits queries whose structure depends on runtime conditions.
// JPQL - fixed at write time TypedQuery<Employee> q1 = em.createQuery( "SELECT e FROM Employee e WHERE e.salary > :min", Employee.class); // EclipseLink native Expression - built dynamically ExpressionBuilder eb = new ExpressionBuilder(); Expression expr = eb.get("salary").greaterThan(minSalary); if (deptFilter != null) { expr = expr.and(eb.get("department").get("name").equal(deptFilter)); } List<Employee> results = (List<Employee>) session.readAllObjects(Employee.class, expr);
The standard JPA Criteria API achieves the same dynamic-query goal in a portable way, at the cost of noticeably more verbose code than either JPQL or EclipseLink's native Expression syntax. EclipseLink's own Expression API predates the JPA Criteria API (again, inherited from TopLink) and is generally considered more concise, but using it directly ties the code to EclipseLink specifically, giving up provider portability in exchange for that readability.
More Related questions...