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 <mayankaggarwal@zeta.tech>
This commit is contained in:
parent
66cca46069
commit
1ce8895891
@ -3,74 +3,81 @@ 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_findAllByEmployeeSalary", query = "from Employee where salary = :employeeSalary")})
|
@org.hibernate.annotations.NamedQuery(name = "Employee_findByEmployeeId", query = "from Employee where id = :employeeId"),
|
||||||
@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.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
|
@Entity
|
||||||
public class Employee implements Serializable {
|
public class Employee implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
private Long salary;
|
private Long salary;
|
||||||
|
|
||||||
// constructors
|
// constructors
|
||||||
public Employee() {
|
public Employee() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Employee(final Integer id, final String name, final Long salary) {
|
public Employee(final Integer id, final String name, final Long salary) {
|
||||||
super();
|
super();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.salary = salary;
|
this.salary = salary;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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)
|
}
|
||||||
return false;
|
if (obj == null) {
|
||||||
if (getClass() != obj.getClass())
|
return false;
|
||||||
return false;
|
}
|
||||||
final Employee other = (Employee) obj;
|
if (getClass() != obj.getClass()) {
|
||||||
if (id == null) {
|
return false;
|
||||||
if (other.id != null)
|
}
|
||||||
return false;
|
final Employee other = (Employee) obj;
|
||||||
} else if (!id.equals(other.id))
|
if (id == null) {
|
||||||
return false;
|
if (other.id != null) {
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else if (!id.equals(other.id)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(Integer id) {
|
public void setId(Integer id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getSalary() {
|
public Long getSalary() {
|
||||||
return salary;
|
return salary;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSalary(Long salary) {
|
public void setSalary(Long salary) {
|
||||||
this.salary = salary;
|
this.salary = salary;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,35 +11,34 @@ import org.hibernate.query.Query;
|
|||||||
|
|
||||||
public class EmployeeCriteriaQueries {
|
public class EmployeeCriteriaQueries {
|
||||||
|
|
||||||
public List<Employee> getAllEmployees() {
|
public List<Employee> getAllEmployees() {
|
||||||
final Session session = HibernateUtil.getHibernateSession();
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||||
final CriteriaQuery<Employee> cr = cb.createQuery(Employee.class);
|
final CriteriaQuery<Employee> cr = cb.createQuery(Employee.class);
|
||||||
final Root<Employee> root = cr.from(Employee.class);
|
final Root<Employee> root = cr.from(Employee.class);
|
||||||
cr.select(root);
|
cr.select(root);
|
||||||
Query<Employee> query = session.createQuery(cr);
|
Query<Employee> query = session.createQuery(cr);
|
||||||
List<Employee> results = query.getResultList();
|
List<Employee> results = query.getResultList();
|
||||||
session.close();
|
session.close();
|
||||||
return results;
|
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();
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,26 +11,26 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class EmployeeCriteriaIntegrationTest {
|
public class EmployeeCriteriaIntegrationTest {
|
||||||
|
|
||||||
final private EmployeeCriteriaQueries employeeCriteriaQueries = new EmployeeCriteriaQueries();
|
final private EmployeeCriteriaQueries employeeCriteriaQueries = new EmployeeCriteriaQueries();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGreaterThanCriteriaQuery() {
|
public void testGreaterThanCriteriaQuery() {
|
||||||
final Session session = HibernateUtil.getHibernateSession();
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
final List<Employee> expectedGreaterThanList = session.createQuery("From Employee where salary>50000").list();
|
final List<Employee> expectedGreaterThanList = session.createQuery("From Employee where salary>50000").list();
|
||||||
final String expectedGreaterThanEmployees[] = new String[expectedGreaterThanList.size()];
|
final String expectedGreaterThanEmployees[] = new String[expectedGreaterThanList.size()];
|
||||||
for (int i = 0; i < expectedGreaterThanList.size(); i++) {
|
for (int i = 0; i < expectedGreaterThanList.size(); i++) {
|
||||||
expectedGreaterThanEmployees[i] = expectedGreaterThanList.get(i).getName();
|
expectedGreaterThanEmployees[i] = expectedGreaterThanList.get(i).getName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
assertArrayEquals(expectedGreaterThanEmployees, employeeCriteriaQueries.greaterThanCriteria());
|
||||||
}
|
}
|
||||||
session.close();
|
|
||||||
assertArrayEquals(expectedGreaterThanEmployees, employeeCriteriaQueries.greaterThanCriteria());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAllEmployeesQuery() {
|
public void testGetAllEmployeesQuery() {
|
||||||
final Session session = HibernateUtil.getHibernateSession();
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
final List<Employee> expectedSortCritEmployeeList = session.createQuery("From Employee").list();
|
final List<Employee> expectedSortCritEmployeeList = session.createQuery("From Employee").list();
|
||||||
session.close();
|
session.close();
|
||||||
assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray());
|
assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<!DOCTYPE hibernate-mapping PUBLIC
|
<!DOCTYPE hibernate-mapping PUBLIC
|
||||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||||
<hibernate-mapping>
|
<hibernate-mapping>
|
||||||
<class name="com.baeldung.hibernate.criteria.model.Employee" table="EMPLOYEE">
|
<class name="com.baeldung.hibernate.criteria.model.Employee" table="EMPLOYEE">
|
||||||
<id name="id" type="java.lang.Integer">
|
<id name="id" type="java.lang.Integer">
|
||||||
<column name="EMPLOYEE_ID" />
|
<column name="EMPLOYEE_ID" />
|
||||||
<generator class="identity" />
|
<generator class="identity" />
|
||||||
</id>
|
</id>
|
||||||
<property name="salary" type="java.lang.Long">
|
<property name="salary" type="java.lang.Long">
|
||||||
<column name="EMPLOYEE_SALARY" not-null="true" />
|
<column name="EMPLOYEE_SALARY" not-null="true" />
|
||||||
</property>
|
</property>
|
||||||
<property name="name" type="string">
|
<property name="name" type="string">
|
||||||
<column name="EMPLOYEE_NAME" not-null="true" />
|
<column name="EMPLOYEE_NAME" not-null="true" />
|
||||||
</property>
|
</property>
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
</hibernate-mapping>
|
</hibernate-mapping>
|
@ -7,12 +7,12 @@ import javax.persistence.Id;
|
|||||||
@Entity
|
@Entity
|
||||||
public class Employee {
|
public class Employee {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private Long salary;
|
private Long salary;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,29 +9,32 @@ import org.springframework.data.repository.query.Param;
|
|||||||
|
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
|
||||||
|
|
||||||
@Query(value = "SELECT e FROM Employee e")
|
@Query(value = "SELECT e FROM Employee e")
|
||||||
List<Employee> findAllEmployees(Sort sort);
|
List<Employee> findAllEmployees(Sort sort);
|
||||||
|
|
||||||
@Query("SELECT e FROM Employee e WHERE e.salary = ?1")
|
@Query("SELECT e FROM Employee e WHERE e.salary = ?1")
|
||||||
Employee findAllEmployeesWithSalary(Long salary);
|
Employee findAllEmployeesWithSalary(Long salary);
|
||||||
|
|
||||||
@Query("SELECT e FROM Employee e WHERE e.name = ?1 and e.salary = ?2")
|
@Query("SELECT e FROM Employee e WHERE e.name = ?1 and e.salary = ?2")
|
||||||
Employee findUserByNameAndSalary(String name, Long salary);
|
Employee findUserByNameAndSalary(String name, Long salary);
|
||||||
|
|
||||||
@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")
|
||||||
Employee findUserByEmployeeNameAndSalaryNamedParameters(
|
Employee findUserByEmployeeNameAndSalaryNamedParameters(
|
||||||
@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",
|
||||||
Employee findUserByNameAndSalaryNamedParamsNative(
|
nativeQuery = true
|
||||||
@Param("name") String employeeName,
|
)
|
||||||
@Param("salary") Long employeeSalary);
|
Employee findUserByNameAndSalaryNamedParamsNative(
|
||||||
|
@Param("name") String employeeName,
|
||||||
|
@Param("salary") Long employeeSalary);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user