From 1ce88958918aa490c1643bf80cc416b7ade79eef Mon Sep 17 00:00:00 2001 From: Mayank Aggarwal Date: Sat, 7 May 2022 17:52:36 +0530 Subject: [PATCH] Bael 5438 (#12176) * [BAEL-5438] Added Criteria Queries for Employee * [BAEL-5438] Added tests and entities for named queries and criteria queries * [BAEL-5438] Removed unused sorting files * [BAEL-5438] Ignored spring context test * BAEL-5438 Indented with 4 spaces Co-authored-by: Mayank Agarwal --- .../hibernate/criteria/model/Employee.java | 121 +++++++++--------- .../view/EmployeeCriteriaQueries.java | 57 ++++----- .../EmployeeCriteriaIntegrationTest.java | 36 +++--- .../hibernate/criteria/model/Employee.hbm.xml | 28 ++-- .../spring/data/jpa/entity/Employee.java | 10 +- .../jpa/repository/EmployeeRepository.java | 41 +++--- 6 files changed, 151 insertions(+), 142 deletions(-) diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java index 994d3f3800..8771e02e0b 100644 --- a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java @@ -3,74 +3,81 @@ package com.baeldung.hibernate.criteria.model; import java.io.Serializable; import javax.persistence.Entity; -@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "Employee_findByEmployeeId", query = "from Employee where id = :employeeId"), - @org.hibernate.annotations.NamedQuery(name = "Employee_findAllByEmployeeSalary", query = "from Employee where salary = :employeeSalary")}) -@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "Employee_FindByEmployeeId", query = "select * from employee emp where employeeId=:employeeId", resultClass = Employee.class)}) +@org.hibernate.annotations.NamedQueries({ + @org.hibernate.annotations.NamedQuery(name = "Employee_findByEmployeeId", query = "from Employee where id = :employeeId"), + @org.hibernate.annotations.NamedQuery(name = "Employee_findAllByEmployeeSalary", query = "from Employee where salary = :employeeSalary")}) +@org.hibernate.annotations.NamedNativeQueries({ + @org.hibernate.annotations.NamedNativeQuery(name = "Employee_FindByEmployeeId", query = "select * from employee emp where employeeId=:employeeId", resultClass = Employee.class)}) @Entity public class Employee implements Serializable { - private static final long serialVersionUID = 1L; - private Integer id; - private String name; - private Long salary; + private static final long serialVersionUID = 1L; + private Integer id; + private String name; + private Long salary; - // constructors - public Employee() { - } + // constructors + public Employee() { + } - public Employee(final Integer id, final String name, final Long salary) { - super(); - this.id = id; - this.name = name; - this.salary = salary; - } + public Employee(final Integer id, final String name, final Long salary) { + super(); + this.id = id; + this.name = name; + this.salary = salary; + } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } - @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final Employee other = (Employee) obj; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Employee other = (Employee) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + return true; + } - public Integer getId() { - return id; - } + public Integer getId() { + return id; + } - public void setId(Integer id) { - this.id = id; - } + public void setId(Integer id) { + this.id = id; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public Long getSalary() { - return salary; - } + public Long getSalary() { + return salary; + } - public void setSalary(Long salary) { - this.salary = salary; - } + public void setSalary(Long salary) { + this.salary = salary; + } } diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java index 04e27d2ec1..f8c525611b 100644 --- a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java @@ -11,35 +11,34 @@ import org.hibernate.query.Query; public class EmployeeCriteriaQueries { - public List getAllEmployees() { - final Session session = HibernateUtil.getHibernateSession(); - final CriteriaBuilder cb = session.getCriteriaBuilder(); - final CriteriaQuery cr = cb.createQuery(Employee.class); - final Root root = cr.from(Employee.class); - cr.select(root); - Query query = session.createQuery(cr); - List results = query.getResultList(); - session.close(); - return results; - } - - // To get items having salary more than 50000 - public String[] greaterThanCriteria() { - final Session session = HibernateUtil.getHibernateSession(); - final CriteriaBuilder cb = session.getCriteriaBuilder(); - final CriteriaQuery cr = cb.createQuery(Employee.class); - final Root root = cr.from(Employee.class); - cr.select(root) - .where(cb.gt(root.get("salary"), 50000)); - Query query = session.createQuery(cr); - final List greaterThanEmployeeList = query.getResultList(); - final String employeeWithGreaterSalary[] = new String[greaterThanEmployeeList.size()]; - for (int i = 0; i < greaterThanEmployeeList.size(); i++) { - employeeWithGreaterSalary[i] = greaterThanEmployeeList.get(i) - .getName(); + public List getAllEmployees() { + final Session session = HibernateUtil.getHibernateSession(); + final CriteriaBuilder cb = session.getCriteriaBuilder(); + final CriteriaQuery cr = cb.createQuery(Employee.class); + final Root root = cr.from(Employee.class); + cr.select(root); + Query query = session.createQuery(cr); + List results = query.getResultList(); + session.close(); + return results; } - session.close(); - return employeeWithGreaterSalary; - } + // To get items having salary more than 50000 + public String[] greaterThanCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + final CriteriaBuilder cb = session.getCriteriaBuilder(); + final CriteriaQuery cr = cb.createQuery(Employee.class); + final Root root = cr.from(Employee.class); + cr.select(root) + .where(cb.gt(root.get("salary"), 50000)); + Query query = session.createQuery(cr); + final List greaterThanEmployeeList = query.getResultList(); + final String employeeWithGreaterSalary[] = new String[greaterThanEmployeeList.size()]; + for (int i = 0; i < greaterThanEmployeeList.size(); i++) { + employeeWithGreaterSalary[i] = greaterThanEmployeeList.get(i) + .getName(); + } + session.close(); + return employeeWithGreaterSalary; + } } diff --git a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java index b2ad4240bf..61f54aaea8 100644 --- a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java +++ b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java @@ -11,26 +11,26 @@ import org.junit.Test; public class EmployeeCriteriaIntegrationTest { - final private EmployeeCriteriaQueries employeeCriteriaQueries = new EmployeeCriteriaQueries(); + final private EmployeeCriteriaQueries employeeCriteriaQueries = new EmployeeCriteriaQueries(); - @Test - public void testGreaterThanCriteriaQuery() { - final Session session = HibernateUtil.getHibernateSession(); - final List expectedGreaterThanList = session.createQuery("From Employee where salary>50000").list(); - final String expectedGreaterThanEmployees[] = new String[expectedGreaterThanList.size()]; - for (int i = 0; i < expectedGreaterThanList.size(); i++) { - expectedGreaterThanEmployees[i] = expectedGreaterThanList.get(i).getName(); + @Test + public void testGreaterThanCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedGreaterThanList = session.createQuery("From Employee where salary>50000").list(); + final String expectedGreaterThanEmployees[] = new String[expectedGreaterThanList.size()]; + for (int i = 0; i < expectedGreaterThanList.size(); i++) { + expectedGreaterThanEmployees[i] = expectedGreaterThanList.get(i).getName(); + } + session.close(); + assertArrayEquals(expectedGreaterThanEmployees, employeeCriteriaQueries.greaterThanCriteria()); } - session.close(); - assertArrayEquals(expectedGreaterThanEmployees, employeeCriteriaQueries.greaterThanCriteria()); - } - @Test - public void testGetAllEmployeesQuery() { - final Session session = HibernateUtil.getHibernateSession(); - final List expectedSortCritEmployeeList = session.createQuery("From Employee").list(); - session.close(); - assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray()); - } + @Test + public void testGetAllEmployeesQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedSortCritEmployeeList = session.createQuery("From Employee").list(); + session.close(); + assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray()); + } } diff --git a/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml b/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml index 90e1c2fefd..0cc1c54680 100644 --- a/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml +++ b/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml @@ -1,19 +1,19 @@ + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java index 214cb09e57..a511686f1b 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java @@ -7,12 +7,12 @@ import javax.persistence.Id; @Entity public class Employee { - @Id - @GeneratedValue - private Integer id; + @Id + @GeneratedValue + private Integer id; - private String name; + private String name; - private Long salary; + private Long salary; } diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java index 652b7b93d2..85d1a1b324 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java @@ -9,29 +9,32 @@ import org.springframework.data.repository.query.Param; public interface EmployeeRepository extends JpaRepository { - @Query(value = "SELECT e FROM Employee e") - List findAllEmployees(Sort sort); + @Query(value = "SELECT e FROM Employee e") + List findAllEmployees(Sort sort); - @Query("SELECT e FROM Employee e WHERE e.salary = ?1") - Employee findAllEmployeesWithSalary(Long salary); + @Query("SELECT e FROM Employee e WHERE e.salary = ?1") + Employee findAllEmployeesWithSalary(Long salary); - @Query("SELECT e FROM Employee e WHERE e.name = ?1 and e.salary = ?2") - Employee findUserByNameAndSalary(String name, Long salary); + @Query("SELECT e FROM Employee e WHERE e.name = ?1 and e.salary = ?2") + Employee findUserByNameAndSalary(String name, Long salary); - @Query( - value = "SELECT * FROM Employee e WHERE e.salary = ?1", - nativeQuery = true) - Employee findUserBySalaryNative(Long salary); + @Query( + value = "SELECT * FROM Employee e WHERE e.salary = ?1", + nativeQuery = true + ) + Employee findUserBySalaryNative(Long salary); - @Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary") - Employee findUserByEmployeeNameAndSalaryNamedParameters( - @Param("name") String employeeName, - @Param("salary") Long employeeSalary); + @Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary") + Employee findUserByEmployeeNameAndSalaryNamedParameters( + @Param("name") String employeeName, + @Param("salary") Long employeeSalary); - @Query(value = "SELECT * FROM Employee e WHERE e.name = :name and e.salary = :salary", - nativeQuery = true) - Employee findUserByNameAndSalaryNamedParamsNative( - @Param("name") String employeeName, - @Param("salary") Long employeeSalary); + @Query( + value = "SELECT * FROM Employee e WHERE e.name = :name and e.salary = :salary", + nativeQuery = true + ) + Employee findUserByNameAndSalaryNamedParamsNative( + @Param("name") String employeeName, + @Param("salary") Long employeeSalary); }