JPQL Named Query
Learn how to write a named query in Java Persistence Query Language (JPQL).
We'll cover the following
A named query is defined on an entity, which in our case is the Player
class. The named query will be used in the getAllPlayers()
method.
@NamedQuery
To create a named query, we will use the @NamedQuery
annotation on the Player
class. This annotation requires two parameters: the name of the query and the query itself. When using JPA, we will write the query in JPQL instead of SQL. JPQL uses entities in place of tables. Since we want to return a list of Player
objects, the query will be "SELECT p FROM Player p"
.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.