Bael 7648 (#16176)
* BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * added new module for get list entity in hibernate * Move the code to hibernate-queries module * simplify the code
This commit is contained in:
parent
c25f863bb2
commit
c957410b7e
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.hibernate.listentity.entity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Department {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
|
||||||
|
private List<Employee> employees;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getEmployees() {
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmployees(List<Employee> employees) {
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.baeldung.hibernate.listentity.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@NamedQuery(name = "findAllEmployees", query = "SELECT e FROM Employee e")
|
||||||
|
@NamedQuery(name = "findEmployeesByDepartment", query = "SELECT e FROM Employee e WHERE e.department = :department ORDER BY e.lastName ASC")
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
private String department;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDepartment() {
|
||||||
|
return department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartment(String department) {
|
||||||
|
this.department = department;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.baeldung.hibernate.listentity.service;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
|
import jakarta.persistence.criteria.CriteriaQuery;
|
||||||
|
import jakarta.persistence.criteria.Path;
|
||||||
|
import jakarta.persistence.criteria.Predicate;
|
||||||
|
import jakarta.persistence.criteria.Root;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.listentity.entity.Department;
|
||||||
|
import com.baeldung.hibernate.listentity.entity.Employee;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class EmployeeService {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesUsingJPQL() {
|
||||||
|
String jpqlQuery = "SELECT e FROM Employee e";
|
||||||
|
Query query = entityManager.createQuery(jpqlQuery, Employee.class);
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesByDepartmentUsingJPQL() {
|
||||||
|
String jpqlQuery = "SELECT e FROM Employee e WHERE e.department = 'Engineering' ORDER BY e.lastName ASC";
|
||||||
|
Query query = entityManager.createQuery(jpqlQuery, Employee.class);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesUsingNamedQuery() {
|
||||||
|
Query query = entityManager.createNamedQuery("findAllEmployees", Employee.class);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesByDepartmentUsingNamedQuery(String department) {
|
||||||
|
Query query = entityManager.createNamedQuery("findEmployeesByDepartment", Employee.class);
|
||||||
|
query.setParameter("department", department);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesUsingCriteriaAPI() {
|
||||||
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
|
||||||
|
Root<Employee> employeeRoot = criteriaQuery.from(Employee.class);
|
||||||
|
|
||||||
|
criteriaQuery.select(employeeRoot);
|
||||||
|
Query query = entityManager.createQuery(criteriaQuery);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesByDepartmentUsingCriteriaAPI(String department) {
|
||||||
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
|
||||||
|
Root<Employee> employeeRoot = criteriaQuery.from(Employee.class);
|
||||||
|
Predicate departmentPredicate = criteriaBuilder.equal(employeeRoot.get("department"), department);
|
||||||
|
Path<Object> sortByPath = employeeRoot.get("lastName");
|
||||||
|
criteriaQuery.orderBy(criteriaBuilder.asc(sortByPath));
|
||||||
|
criteriaQuery.select(employeeRoot)
|
||||||
|
.where(departmentPredicate);
|
||||||
|
Query query = entityManager.createQuery(criteriaQuery);
|
||||||
|
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getAllEmployeesByDepartmentUsingOneToManyAnnotations(String departmentName) {
|
||||||
|
try {
|
||||||
|
// Retrieve the department by its name
|
||||||
|
Department department = entityManager.createQuery("SELECT d FROM Department d WHERE d.name = :name", Department.class)
|
||||||
|
.setParameter("name", departmentName)
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
// Return the list of employees associated with the department
|
||||||
|
return department.getEmployees();
|
||||||
|
} catch (NoResultException e) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue