* [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 <mayankaggarwal@zeta.tech>
This commit is contained in:
Mayank Aggarwal 2022-05-07 17:52:36 +05:30 committed by GitHub
parent 66cca46069
commit 1ce8895891
6 changed files with 151 additions and 142 deletions

View File

@ -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;
}
}

View File

@ -11,35 +11,34 @@ import org.hibernate.query.Query;
public class EmployeeCriteriaQueries {
public List<Employee> getAllEmployees() {
final Session session = HibernateUtil.getHibernateSession();
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Employee> cr = cb.createQuery(Employee.class);
final Root<Employee> root = cr.from(Employee.class);
cr.select(root);
Query<Employee> query = session.createQuery(cr);
List<Employee> 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<Employee> cr = cb.createQuery(Employee.class);
final Root<Employee> root = cr.from(Employee.class);
cr.select(root)
.where(cb.gt(root.get("salary"), 50000));
Query<Employee> query = session.createQuery(cr);
final List<Employee> 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<Employee> getAllEmployees() {
final Session session = HibernateUtil.getHibernateSession();
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Employee> cr = cb.createQuery(Employee.class);
final Root<Employee> root = cr.from(Employee.class);
cr.select(root);
Query<Employee> query = session.createQuery(cr);
List<Employee> 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<Employee> cr = cb.createQuery(Employee.class);
final Root<Employee> root = cr.from(Employee.class);
cr.select(root)
.where(cb.gt(root.get("salary"), 50000));
Query<Employee> query = session.createQuery(cr);
final List<Employee> 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;
}
}

View File

@ -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<Employee> 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<Employee> 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<Employee> 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<Employee> expectedSortCritEmployeeList = session.createQuery("From Employee").list();
session.close();
assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray());
}
}

View File

@ -1,19 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.baeldung.hibernate.criteria.model.Employee" table="EMPLOYEE">
<id name="id" type="java.lang.Integer">
<column name="EMPLOYEE_ID" />
<generator class="identity" />
</id>
<property name="salary" type="java.lang.Long">
<column name="EMPLOYEE_SALARY" not-null="true" />
</property>
<property name="name" type="string">
<column name="EMPLOYEE_NAME" not-null="true" />
</property>
</class>
<class name="com.baeldung.hibernate.criteria.model.Employee" table="EMPLOYEE">
<id name="id" type="java.lang.Integer">
<column name="EMPLOYEE_ID" />
<generator class="identity" />
</id>
<property name="salary" type="java.lang.Long">
<column name="EMPLOYEE_SALARY" not-null="true" />
</property>
<property name="name" type="string">
<column name="EMPLOYEE_NAME" not-null="true" />
</property>
</class>
</hibernate-mapping>

View File

@ -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;
}

View File

@ -9,29 +9,32 @@ import org.springframework.data.repository.query.Param;
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query(value = "SELECT e FROM Employee e")
List<Employee> findAllEmployees(Sort sort);
@Query(value = "SELECT e FROM Employee e")
List<Employee> 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);
}