Justin
Justin

Reputation: 415

What is JPQL, what does it do, and what is it used for?

What are the uses of JPQL? I'm fairly new to JPA. Doesn't the EntityManager perform the CRUD operations for you? What is JPQL used for?

Upvotes: 0

Views: 548

Answers (2)

davidxxx
davidxxx

Reputation: 131396

Doesn't the EntityManager perform the basic CRUD operations for you?

It does. But it is not always enough.
The EntityManager provides indeed basic CRUD operations on the entity such as (without being exhaustive) inserting, selecting from its id, deleting, updating entities, respectively : EntityManager.persist(), EntityManager.find(), EntityManager.remove(), EntityManager.merge().
In very simple cases, these are enough but for more specific requirements, you need a way to custom your queries such as an update on some specific fields or a select with a specific conditions or that returns something else than an entity.
In this case, JPQL is a way to do that.
Note that it is not the single way : as alternative you can use the Criteria API (EntityManager.createQuery(CriteriaQuery)) that is an object based API or still use native SQL query (EntityManager.createNativeQuery(String)) while the last should be used only for good reasons.

Upvotes: 2

Keaz
Keaz

Reputation: 985

JPQL is database independent query language. Sometimes you have to write custom queries like "Select Customer from firstName like 'ss%' and lastName like 'ss%'" other than getting object from primary key so then you have to use JPQL.

Upvotes: 0

Related Questions