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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@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;
Expand Down Expand Up @@ -76,4 +76,4 @@ public void addSpecialty(Specialty specialty) {
getSpecialtiesInternal().add(specialty);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -41,6 +43,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;
Expand All @@ -51,8 +54,9 @@ public interface VetRepository extends Repository<Vet, Integer> {
* @return
* @throws DataAccessException
*/
@EntityGraph(attributePaths = {"specialties"})
@Transactional(readOnly = true)
@Cacheable("vets")
Page<Vet> findAll(Pageable pageable) throws DataAccessException;

}
}
6 changes: 4 additions & 2 deletions src/main/resources/db/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ CREATE TABLE IF NOT EXISTS vet_specialties (
specialty_id INT(4) UNSIGNED NOT NULL,
FOREIGN KEY (vet_id) REFERENCES vets(id),
FOREIGN KEY (specialty_id) REFERENCES specialties(id),
UNIQUE (vet_id,specialty_id)
UNIQUE (vet_id,specialty_id),
INDEX idx_vet_specialties_vet (vet_id),
INDEX idx_vet_specialties_specialty (specialty_id)
) engine=InnoDB;

CREATE TABLE IF NOT EXISTS types (
Expand Down Expand Up @@ -52,4 +54,4 @@ CREATE TABLE IF NOT EXISTS visits (
visit_date DATE,
description VARCHAR(255),
FOREIGN KEY (pet_id) REFERENCES pets(id)
) engine=InnoDB;
) engine=InnoDB;