From 11a41d5036b2512635ef59c93c8f0c2d980d4df6 Mon Sep 17 00:00:00 2001 From: glopez Date: Thu, 13 Jun 2019 22:53:57 -0300 Subject: [PATCH 1/4] BAEL-2804 JPA Query Parameters Usage This is the code behind 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. --- persistence-modules/java-jpa/pom.xml | 57 ++++++++++++ .../baeldung/jpa/queryparams/Employee.java | 80 +++++++++++++++++ .../main/resources/META-INF/persistence.xml | 22 +++++ .../queryparams/JPAQueryParamsUnitTest.java | 90 +++++++++++++++++++ .../src/test/resources/employees2.sql | 2 + 5 files changed, 251 insertions(+) create mode 100644 persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java create mode 100644 persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java create mode 100644 persistence-modules/java-jpa/src/test/resources/employees2.sql diff --git a/persistence-modules/java-jpa/pom.xml b/persistence-modules/java-jpa/pom.xml index f23040fbdc..51cc401332 100644 --- a/persistence-modules/java-jpa/pom.xml +++ b/persistence-modules/java-jpa/pom.xml @@ -19,6 +19,11 @@ hibernate-core ${hibernate.version} + + org.hibernate + hibernate-jpamodelgen + ${hibernate.version} + com.h2database h2 @@ -47,6 +52,58 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + -proc:none + + + + org.bsc.maven + maven-processor-plugin + 3.3.3 + + + process + + process + + generate-sources + + target/metamodel + + org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor + + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + add-source + generate-sources + + add-source + + + + target/metamodel + + + + + + + 5.4.0.Final 2.7.4-RC1 diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java new file mode 100644 index 0000000000..e8f4a10156 --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java @@ -0,0 +1,80 @@ +package com.baeldung.jpa.queryparams; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + + +@Entity +@Table(name = "employees") +public class Employee { + + /** + * + */ + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "employee_number", unique = true) + private String empNumber; + + @Column(name = "employee_name") + private String name; + + @Column(name = "employee_age") + private int age; + + public Employee() { + super(); + } + + public Employee(Long id, String empNumber) { + super(); + this.id = id; + this.empNumber = empNumber; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmpNumber() { + return empNumber; + } + + public void setEmpNumber(String empNumber) { + this.empNumber = empNumber; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } + +} diff --git a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml index 1f16bee3ba..6a236f0840 100644 --- a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml @@ -224,4 +224,26 @@ value="products_jpa.sql" /> + + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.queryparams.Employee + true + + + + + + + + + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..151a06a278 --- /dev/null +++ b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.jpa.queryparams; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.ParameterExpression; +import javax.persistence.criteria.Root; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * JPAQueryParamsTest class tests. + * + * @author gmlopez.mackinnon@gmail.com + */ +public class JPAQueryParamsUnitTest { + + private static EntityManager entityManager; + + @BeforeClass + public static void setup() { + EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-queryparams"); + entityManager = factory.createEntityManager(); + } + + @Test + public void givenEmpNumber_whenUsingPositionalParameter_thenReturnExpectedEmployee() { + TypedQuery query = entityManager.createQuery( + "SELECT e FROM Employee e WHERE e.empNumber = ?0" , Employee.class); + String empNumber = "A123"; + Employee employee = query.setParameter(0, empNumber).getSingleResult(); + Assert.assertNotNull("Employee not found", employee); + } + + @Test + public void givenEmpNumber_whenUsingNamedParameter_thenReturnExpectedEmployee() { + TypedQuery query = entityManager.createQuery( + "SELECT e FROM Employee e WHERE e.empNumber = :number" , Employee.class); + String empNumber = "A123"; + Employee employee = query.setParameter("number", empNumber).getSingleResult(); + Assert.assertNotNull("Employee not found", employee); + } + + @Test + public void givenEmpNumber_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"; + int empAge = 55; + List employees = query + .setParameter("name", empName) + .setParameter("empAge", empAge) + .getResultList(); + Assert.assertNotNull("Employees not found!", employees); + Assert.assertTrue("Employees not found!", !employees.isEmpty()); + } + + @Test + public void givenEmpNumber_whenUsingCriteriaQuery_thenReturnExpectedEmployee() { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + + CriteriaQuery cQuery = cb.createQuery(Employee.class); + Root c = cQuery.from(Employee.class); + ParameterExpression paramEmpNumber = cb.parameter(String.class); + cQuery.select(c).where(cb.equal(c.get(Employee_.empNumber), paramEmpNumber)); + + TypedQuery query = entityManager.createQuery(cQuery); + String empNumber = "A123"; + query.setParameter(paramEmpNumber, empNumber); + Employee employee = query.getSingleResult(); + Assert.assertNotNull("Employee not found!", employee); + } + + @Test + public void givenEmpNumber_whenUsingLiteral_thenReturnExpectedEmployee() { + String empNumber = "A123"; + TypedQuery query = entityManager.createQuery( + "SELECT e FROM Employee e WHERE e.empNumber = '" + empNumber + "'", Employee.class); + Employee employee = query.getSingleResult(); + Assert.assertNotNull("Employee not found!", employee); + } + +} diff --git a/persistence-modules/java-jpa/src/test/resources/employees2.sql b/persistence-modules/java-jpa/src/test/resources/employees2.sql new file mode 100644 index 0000000000..d3ae46f6a0 --- /dev/null +++ b/persistence-modules/java-jpa/src/test/resources/employees2.sql @@ -0,0 +1,2 @@ +INSERT INTO employees (employee_number, employee_name, employee_age) VALUES ('111', 'John Doe', 55); +INSERT INTO employees (employee_number, employee_name, employee_age) VALUES ('A123', 'John Doe Junior', 25); \ No newline at end of file From 6aec4a6e0f5c92e1becc0a0d26d88b3ad57aeaa1 Mon Sep 17 00:00:00 2001 From: glopez Date: Sun, 16 Jun 2019 00:37:44 -0300 Subject: [PATCH 2/4] 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"; From 07adfc9e7037774c0fb0c7a26768b8b17df1d2df Mon Sep 17 00:00:00 2001 From: glopez Date: Mon, 17 Jun 2019 07:42:48 -0300 Subject: [PATCH 3/4] BAEL-2804 JPA Query Parameters Usage Fix givenEmpNumber_whenUsingPositionalParameter_thenReturnExpectedEmployee test case after adjustemnts. --- .../com/baeldung/jpa/queryparams/JPAQueryParamsUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fcac6415c9..97f82f7914 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 @@ -37,7 +37,7 @@ public class JPAQueryParamsUnitTest { TypedQuery query = entityManager.createQuery( "SELECT e FROM Employee e WHERE e.empNumber = ?1" , Employee.class); String empNumber = "A123"; - Employee employee = query.setParameter(0, empNumber).getSingleResult(); + Employee employee = query.setParameter(1, empNumber).getSingleResult(); Assert.assertNotNull("Employee not found", employee); } From 0ef4938069fcdde02f42ae0bd95217f64d4511c3 Mon Sep 17 00:00:00 2001 From: glopez Date: Wed, 19 Jun 2019 20:15:56 -0300 Subject: [PATCH 4/4] BAEL-2804 JPA Query Parameters Usage - formatting style Fix formatting style. --- .../baeldung/jpa/queryparams/Employee.java | 105 +++++++++-------- .../queryparams/JPAQueryParamsUnitTest.java | 107 +++++++++--------- 2 files changed, 104 insertions(+), 108 deletions(-) diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java index e8f4a10156..bf3d459530 100644 --- a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/queryparams/Employee.java @@ -7,74 +7,73 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; - @Entity @Table(name = "employees") public class Employee { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id") - private Long id; - - @Column(name = "employee_number", unique = true) - private String empNumber; - - @Column(name = "employee_name") - private String name; - - @Column(name = "employee_age") - private int age; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; - public Employee() { - super(); - } + @Column(name = "employee_number", unique = true) + private String empNumber; - public Employee(Long id, String empNumber) { - super(); - this.id = id; - this.empNumber = empNumber; - } + @Column(name = "employee_name") + private String name; - public Long getId() { - return id; - } + @Column(name = "employee_age") + private int age; - public void setId(Long id) { - this.id = id; - } + public Employee() { + super(); + } - public String getName() { - return name; - } + public Employee(Long id, String empNumber) { + super(); + this.id = id; + this.empNumber = empNumber; + } - public int getAge() { - return age; - } + public Long getId() { + return id; + } - public void setAge(int age) { - this.age = age; - } + public void setId(Long id) { + this.id = id; + } - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } - public String getEmpNumber() { - return empNumber; - } + public int getAge() { + return age; + } - public void setEmpNumber(String empNumber) { - this.empNumber = empNumber; - } + public void setAge(int age) { + this.age = age; + } - public static long getSerialversionuid() { - return serialVersionUID; - } + public void setName(String name) { + this.name = name; + } + + public String getEmpNumber() { + return empNumber; + } + + public void setEmpNumber(String empNumber) { + this.empNumber = empNumber; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } } 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 97f82f7914..4f320935cf 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,7 +1,6 @@ package com.baeldung.jpa.queryparams; import java.util.Arrays; -import java.util.Collections; import java.util.List; import javax.persistence.EntityManager; @@ -24,89 +23,87 @@ import org.junit.Test; */ public class JPAQueryParamsUnitTest { - private static EntityManager entityManager; - - @BeforeClass - public static void setup() { + private static EntityManager entityManager; + + @BeforeClass + public static void setup() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-queryparams"); entityManager = factory.createEntityManager(); - } + } - @Test + @Test public void givenEmpNumber_whenUsingPositionalParameter_thenReturnExpectedEmployee() { - TypedQuery query = entityManager.createQuery( - "SELECT e FROM Employee e WHERE e.empNumber = ?1" , Employee.class); - String empNumber = "A123"; - Employee employee = query.setParameter(1, empNumber).getSingleResult(); + TypedQuery query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = ?1", Employee.class); + String empNumber = "A123"; + Employee employee = query.setParameter(1, empNumber) + .getSingleResult(); Assert.assertNotNull("Employee not found", employee); } - @Test + @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(); + 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( - "SELECT e FROM Employee e WHERE e.empNumber = :number" , Employee.class); - String empNumber = "A123"; - Employee employee = query.setParameter("number", empNumber).getSingleResult(); - Assert.assertNotNull("Employee not found", employee); + TypedQuery query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = :number", Employee.class); + String empNumber = "A123"; + Employee employee = query.setParameter("number", empNumber) + .getSingleResult(); + Assert.assertNotNull("Employee not found", employee); } @Test 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()); + 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"; - int empAge = 55; - List employees = query - .setParameter("name", empName) - .setParameter("empAge", empAge) - .getResultList(); - Assert.assertNotNull("Employees not found!", employees); - Assert.assertTrue("Employees not found!", !employees.isEmpty()); + TypedQuery query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.name = :name AND e.age = :empAge", Employee.class); + String empName = "John Doe"; + int empAge = 55; + List employees = query.setParameter("name", empName) + .setParameter("empAge", empAge) + .getResultList(); + Assert.assertNotNull("Employees not found!", employees); + Assert.assertTrue("Employees not found!", !employees.isEmpty()); } @Test public void givenEmpNumber_whenUsingCriteriaQuery_thenReturnExpectedEmployee() { - CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); - CriteriaQuery cQuery = cb.createQuery(Employee.class); - Root c = cQuery.from(Employee.class); - ParameterExpression paramEmpNumber = cb.parameter(String.class); - cQuery.select(c).where(cb.equal(c.get(Employee_.empNumber), paramEmpNumber)); - - TypedQuery query = entityManager.createQuery(cQuery); - String empNumber = "A123"; - query.setParameter(paramEmpNumber, empNumber); - Employee employee = query.getSingleResult(); - Assert.assertNotNull("Employee not found!", employee); + CriteriaQuery cQuery = cb.createQuery(Employee.class); + Root c = cQuery.from(Employee.class); + ParameterExpression paramEmpNumber = cb.parameter(String.class); + cQuery.select(c) + .where(cb.equal(c.get(Employee_.empNumber), paramEmpNumber)); + + TypedQuery query = entityManager.createQuery(cQuery); + String empNumber = "A123"; + query.setParameter(paramEmpNumber, empNumber); + Employee employee = query.getSingleResult(); + Assert.assertNotNull("Employee not found!", employee); } @Test public void givenEmpNumber_whenUsingLiteral_thenReturnExpectedEmployee() { - String empNumber = "A123"; - TypedQuery query = entityManager.createQuery( - "SELECT e FROM Employee e WHERE e.empNumber = '" + empNumber + "'", Employee.class); - Employee employee = query.getSingleResult(); - Assert.assertNotNull("Employee not found!", employee); + String empNumber = "A123"; + TypedQuery query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = '" + empNumber + "'", Employee.class); + Employee employee = query.getSingleResult(); + Assert.assertNotNull("Employee not found!", employee); } }