From 6aec4a6e0f5c92e1becc0a0d26d88b3ad57aeaa1 Mon Sep 17 00:00:00 2001 From: glopez Date: Sun, 16 Jun 2019 00:37:44 -0300 Subject: [PATCH] BAEL-2804 JPA Query Parameters Usage This is the code behind the JPA Query Parameters Usage tutorial. Within these examples you can find the way to use named and positional query parameters either using plain JPQL or criteria queries. --- .../queryparams/JPAQueryParamsUnitTest.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java index 151a06a278..fcac6415c9 100644 --- a/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java +++ b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java @@ -1,5 +1,7 @@ package com.baeldung.jpa.queryparams; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import javax.persistence.EntityManager; @@ -33,12 +35,22 @@ public class JPAQueryParamsUnitTest { @Test public void givenEmpNumber_whenUsingPositionalParameter_thenReturnExpectedEmployee() { TypedQuery query = entityManager.createQuery( - "SELECT e FROM Employee e WHERE e.empNumber = ?0" , Employee.class); + "SELECT e FROM Employee e WHERE e.empNumber = ?1" , Employee.class); String empNumber = "A123"; Employee employee = query.setParameter(0, empNumber).getSingleResult(); Assert.assertNotNull("Employee not found", employee); } + @Test + public void givenEmpNumberList_whenUsingPositionalParameter_thenReturnExpectedEmployee() { + TypedQuery query = entityManager.createQuery( + "SELECT e FROM Employee e WHERE e.empNumber IN (?1)" , Employee.class); + List empNumbers = Arrays.asList("A123", "A124"); + List employees = query.setParameter(1, empNumbers).getResultList(); + Assert.assertNotNull("Employees not found", employees); + Assert.assertFalse("Employees not found", employees.isEmpty()); + } + @Test public void givenEmpNumber_whenUsingNamedParameter_thenReturnExpectedEmployee() { TypedQuery query = entityManager.createQuery( @@ -49,7 +61,17 @@ public class JPAQueryParamsUnitTest { } @Test - public void givenEmpNumber_whenUsingTwoNamedParameters_thenReturnExpectedEmployees() { + public void givenEmpNumberList_whenUsingNamedParameter_thenReturnExpectedEmployee() { + TypedQuery query = entityManager.createQuery( + "SELECT e FROM Employee e WHERE e.empNumber IN (:numbers)" , Employee.class); + List empNumbers = Arrays.asList("A123", "A124"); + List employees = query.setParameter("numbers", empNumbers).getResultList(); + Assert.assertNotNull("Employees not found", employees); + Assert.assertFalse("Employees not found", employees.isEmpty()); + } + + @Test + public void givenEmpNameAndEmpAge_whenUsingTwoNamedParameters_thenReturnExpectedEmployees() { TypedQuery query = entityManager.createQuery( "SELECT e FROM Employee e WHERE e.name = :name AND e.age = :empAge" , Employee.class); String empName = "John Doe";