mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-12-27 19:07:28 +00:00
feat: optimize JPA performance with lazy loading and fetch joins
Signed-off-by: Mickael-7 <mickael.albuquerque@upe.br>
This commit is contained in:
parent
2969add25c
commit
5c2d7f23f5
5 changed files with 8 additions and 3 deletions
|
|
@ -61,7 +61,7 @@ public class Owner extends Person {
|
|||
@Pattern(regexp = "\\d{10}", message = "{telephone.invalid}")
|
||||
private String telephone;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "owner_id")
|
||||
@OrderBy("name")
|
||||
private final List<Pet> pets = new ArrayList<>();
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.util.Optional;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
/**
|
||||
* Repository class for <code>Owner</code> domain objects. All method names are compliant
|
||||
|
|
@ -57,6 +58,7 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
|
|||
* @throws IllegalArgumentException if the id is null (assuming null is not a valid
|
||||
* input for id)
|
||||
*/
|
||||
@Query("SELECT DISTINCT owner FROM Owner owner LEFT JOIN FETCH owner.pets p LEFT JOIN FETCH p.visits WHERE owner.id = :id")
|
||||
Optional<Owner> findById(Integer id);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ public class Pet extends NamedEntity {
|
|||
@JoinColumn(name = "type_id")
|
||||
private PetType type;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "pet_id")
|
||||
@OrderBy("date ASC")
|
||||
private final Set<Visit> visits = new LinkedHashSet<>();
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ import jakarta.xml.bind.annotation.XmlElement;
|
|||
@Table(name = "vets")
|
||||
public class Vet extends Person {
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
|
||||
private Set<Specialty> specialties;
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import org.springframework.cache.annotation.Cacheable;
|
|||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
|
@ -41,6 +42,7 @@ public interface VetRepository extends Repository<Vet, Integer> {
|
|||
* Retrieve all <code>Vet</code>s from the data store.
|
||||
* @return a <code>Collection</code> of <code>Vet</code>s
|
||||
*/
|
||||
@Query("SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties")
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable("vets")
|
||||
Collection<Vet> findAll() throws DataAccessException;
|
||||
|
|
@ -51,6 +53,7 @@ public interface VetRepository extends Repository<Vet, Integer> {
|
|||
* @return
|
||||
* @throws DataAccessException
|
||||
*/
|
||||
@Query("SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties")
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable("vets")
|
||||
Page<Vet> findAll(Pageable pageable) throws DataAccessException;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue