0

I am using JPA with below method.

    Query query = entityManager.createQuery(sqlQuery);
    queryBuilder.fillParametersToQuery(query);

I want to get SQL query string with all placeholder values .

Jens
  • 67,715
  • 15
  • 98
  • 113
  • 2
    maybe duplicate?https://stackoverflow.com/questions/1710476/how-to-print-a-query-string-with-parameter-values-when-using-hibernate – Michal May 29 '18 at 13:24
  • 1
    FWIW The query you pass to `createQuery` is *not* SQL –  May 29 '18 at 15:11

1 Answers1

2

JPA Query Parameters

JPA Query Parameters come in two flavours

  1. Positional parameter notation
  2. 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

SELECT e
FROM Employee e
WHERE e.salary > ?1

SELECT e FROM Employee e WHERE e.salary > ?1 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.

query.setParameter(1, salary).getResultList();

query.setParameter(1, salary).getResultList(); 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

SELECT e
FROM Employee e
WHERE e.name = :name

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

query.setParameter("name",employeeName);

query.setParameter("name",employeeName); Here, name is the parameter name specified in query and employeeName is the variable of type String.

Vinit Mehta
  • 449
  • 4
  • 13