JPA Query Parameters

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have learned about simple JPA queries in previous articles, We can write JPQL to retrieve managed entities but what if we want to retrieve some data based on runtime information? What if the information needed for queries is at runtime? In this case JPA Query Parameters are useful. JPA query parameters allow the queries to have placeholders in them.

JPA Query Parameters

JPA Query Parameters come in two flavours

  • Positional parameter notation
  • Named parameter notation

Positional parameters notation:

As the name suggests, JPA query parameters are shown by numbers, the example of this type of notation will be

Here, ?1 is the positional parameter and 1 is its position. If you want to execute this query then you have to set the parameter value using position of parameter.

This statement sets value of JPA query parameters specified in the JPQL and executes query.

Named Parameters Notation

JPA Query Parameters with named parameters notation are as follows

Here, :name is named parameter. It will hold the value of name passed at runtime. Parameters are passed to such queries as follows

Here, name is the parameter name specified in query and employeeName is the variable of type String.

I hope this article helped understand jpa query parameters.

 

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *