Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/main/java/org/springframework/samples/petclinic/vet/Vet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.samples.petclinic.model.Person;

import jakarta.persistence.Entity;
import org.springframework.samples.petclinic.model.Person;import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
Expand All @@ -42,10 +40,9 @@
* @author Arjen Poutsma
*/
@Entity
@Table(name = "vets")
public class Vet extends Person {
@Table(name = "vets")public class Vet extends Person {

@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany(fetch = FetchType.LAZY) // Changed from EAGER to LAZY to prevent N+1 queries
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
private Set<Specialty> specialties;
Expand Down Expand Up @@ -76,4 +73,4 @@ public void addSpecialty(Specialty specialty) {
getSpecialtiesInternal().add(specialty);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import java.util.Collection;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;

/**
* Repository class for <code>Vet</code> domain objects All method names are compliant
Expand All @@ -34,7 +37,9 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
*/import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;

public interface VetRepository extends Repository<Vet, Integer> {

/**
Expand All @@ -43,6 +48,7 @@ public interface VetRepository extends Repository<Vet, Integer> {
*/
@Transactional(readOnly = true)
@Cacheable("vets")
@EntityGraph(attributePaths = {"specialties"})
Collection<Vet> findAll() throws DataAccessException;

/**
Expand All @@ -53,6 +59,11 @@ public interface VetRepository extends Repository<Vet, Integer> {
*/
@Transactional(readOnly = true)
@Cacheable("vets")
@EntityGraph(attributePaths = {"specialties"})
Page<Vet> findAll(Pageable pageable) throws DataAccessException;

}
@EntityGraph(attributePaths = {"specialties"})
@Query("SELECT DISTINCT vet FROM Vet vet LEFT JOIN FETCH vet.specialties")
Collection<Vet> findAllWithSpecialties() throws DataAccessException;

}