Sobes.tech
Junior — Middle

Can you explain what JPQL is and how it differs from SQL?

sobes.tech AI

Answer from AI

JPQL (Java Persistence Query Language) is an object-oriented query language used in JPA (Java Persistence API) for working with databases through Java objects.

Main differences between JPQL and SQL:

  • JPQL works with entities (objects), not directly with tables and columns.
  • JPQL queries return objects or their properties, not raw data from tables.
  • The syntax is similar to SQL but oriented towards classes and their fields.

Example of a JPQL query:

String jpql = "SELECT u FROM User u WHERE u.age > 18";
List<User> adults = entityManager.createQuery(jpql, User.class).getResultList();

Here, User is an entity class, not a database table. JPQL allows writing queries that are independent of a specific DBMS.