* [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,9 +3,11 @@ package com.baeldung.hibernate.criteria.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Entity; import javax.persistence.Entity;
@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "Employee_findByEmployeeId", query = "from Employee where id = :employeeId"), @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.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.NamedNativeQueries({
@org.hibernate.annotations.NamedNativeQuery(name = "Employee_FindByEmployeeId", query = "select * from employee emp where employeeId=:employeeId", resultClass = Employee.class)})
@Entity @Entity
public class Employee implements Serializable { public class Employee implements Serializable {
@ -35,18 +37,23 @@ public class Employee implements Serializable {
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
if (this == obj) if (this == obj) {
return true; return true;
if (obj == null) }
if (obj == null) {
return false; return false;
if (getClass() != obj.getClass()) }
if (getClass() != obj.getClass()) {
return false; return false;
}
final Employee other = (Employee) obj; final Employee other = (Employee) obj;
if (id == null) { if (id == null) {
if (other.id != null) if (other.id != null) {
return false; return false;
} else if (!id.equals(other.id)) }
} else if (!id.equals(other.id)) {
return false; return false;
}
return true; return true;
} }

View File

@ -41,5 +41,4 @@ public class EmployeeCriteriaQueries {
session.close(); session.close();
return employeeWithGreaterSalary; return employeeWithGreaterSalary;
} }
} }

View File

@ -20,7 +20,8 @@ public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query( @Query(
value = "SELECT * FROM Employee e WHERE e.salary = ?1", value = "SELECT * FROM Employee e WHERE e.salary = ?1",
nativeQuery = true) nativeQuery = true
)
Employee findUserBySalaryNative(Long salary); Employee findUserBySalaryNative(Long salary);
@Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary") @Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary")
@ -28,8 +29,10 @@ public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Param("name") String employeeName, @Param("name") String employeeName,
@Param("salary") Long employeeSalary); @Param("salary") Long employeeSalary);
@Query(value = "SELECT * FROM Employee e WHERE e.name = :name and e.salary = :salary", @Query(
nativeQuery = true) value = "SELECT * FROM Employee e WHERE e.name = :name and e.salary = :salary",
nativeQuery = true
)
Employee findUserByNameAndSalaryNamedParamsNative( Employee findUserByNameAndSalaryNamedParamsNative(
@Param("name") String employeeName, @Param("name") String employeeName,
@Param("salary") Long employeeSalary); @Param("salary") Long employeeSalary);