Сообщения

N + 1 problem and solution

* The N + 1 select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number of child objects fetched). For example, if N = 2, the application makes 3 (N + 1 = 3) database calls. package com.example.cascade_types_and_n_plus_one_problem.entity ; import jakarta.persistence.* ; import lombok.* ; import org.hibernate.Hibernate ; import java.util.ArrayList ; import java.util.List ; import java.util.Objects ; @Getter @Setter @ToString @RequiredArgsConstructor @Entity public class Department { @Id @GeneratedValue (strategy = GenerationType. IDENTITY ) private Long id ; private String name ; @OneToMany (mappedBy = "department" , cascade = CascadeType. ALL , fetch = FetchType. EAGER ) private List<Employee> employees = new ArrayList<>() ; @Override public boolean equals (Object o) { if ( this == o) return true; if (o == null || Hibernate. getClass ( this ) != Hiber...

JPA Specification, Dynamic queries

1. Simple Specification: Student class: package az.etibarli.jpa_specification.entity ; import jakarta.persistence. Entity ; import jakarta.persistence. GeneratedValue ; import jakarta.persistence.GenerationType ; import jakarta.persistence. Id ; import lombok. Data ; import lombok. RequiredArgsConstructor ; import lombok.experimental. FieldNameConstants ; import java.math.BigDecimal ; import java.time.LocalDate ; @Data @Entity @FieldNameConstants @RequiredArgsConstructor public class Student { @Id @GeneratedValue (strategy = GenerationType. IDENTITY ) private Long id ; private String name ; private String surname ; private String pinCode ; private Double gpa ; private LocalDate birthdate ; private BigDecimal scholarship ; } Student repository: package az.etibarli.jpa_specification.repository ; import az.etibarli.jpa_specification.entity.Student ; import org.springframework.data.jpa.repository.JpaRepository ; import org.springframework.data.jpa.reposi...