Interface RaceRepository
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<Race,,Long> org.springframework.data.jpa.repository.JpaRepository<@NonNull Race,,@NonNull Long> org.springframework.data.repository.ListCrudRepository<Race,,Long> org.springframework.data.repository.ListPagingAndSortingRepository<Race,,Long> org.springframework.data.repository.PagingAndSortingRepository<Race,,Long> org.springframework.data.repository.query.QueryByExampleExecutor<Race>,org.springframework.data.repository.Repository<Race,Long>
@Repository
public interface RaceRepository
extends org.springframework.data.jpa.repository.JpaRepository<@NonNull Race,@NonNull Long>
The
RaceRepository provides database access for Race entities.-
Method Summary
Modifier and TypeMethodDescriptionFinds aRaceby its ID with its courses and their distances eagerly fetched, avoiding N+1 queries when accessing the full race object graph.org.springframework.data.domain.Page<Long> findNonDerivedRaceIdsWithMostRecentAddedResults(org.springframework.data.domain.Pageable pageable) Returns aPageof IDs of non-derivedracesthat have at least oneIndividualResultorTeamResultrecorded in the database.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findByIdWithGraph
@Query("SELECT r FROM Race r LEFT JOIN FETCH r.courses c LEFT JOIN FETCH c.distance WHERE r.id = :id") Optional<Race> findByIdWithGraph(@Param("id") Long id) Finds aRaceby its ID with its courses and their distances eagerly fetched, avoiding N+1 queries when accessing the full race object graph.- Parameters:
id- the ID of the race to find- Returns:
- the
Racewith its courses and distances loaded, or empty if not found
-
findNonDerivedRaceIdsWithMostRecentAddedResults
@Query(value="SELECT r.id FROM Race r WHERE r.raceType NOT IN (\'INDIVIDUAL_DERIVED\', \'TEAM_RELAY_DERIVED\', \'MIXED_RELAY_DERIVED\') AND (r.id IN (SELECT ir.race.id FROM IndividualResult ir) OR r.id IN (SELECT tr.race.id FROM TeamResult tr)) ORDER BY COALESCE( (SELECT MAX(ir2.createdAt) FROM IndividualResult ir2 WHERE ir2.race = r), (SELECT MAX(tr2.createdAt) FROM TeamResult tr2 WHERE tr2.race = r)) DESC, r.id DESC", countQuery="SELECT COUNT(r.id) FROM Race r WHERE r.raceType NOT IN (\'INDIVIDUAL_DERIVED\', \'TEAM_RELAY_DERIVED\', \'MIXED_RELAY_DERIVED\') AND (r.id IN (SELECT ir.race.id FROM IndividualResult ir) OR r.id IN (SELECT tr.race.id FROM TeamResult tr))") org.springframework.data.domain.Page<Long> findNonDerivedRaceIdsWithMostRecentAddedResults(org.springframework.data.domain.Pageable pageable) Returns aPageof IDs of non-derivedracesthat have at least oneIndividualResultorTeamResultrecorded in the database.Derived race types (
INDIVIDUAL_DERIVED,TEAM_RELAY_DERIVED,MIXED_RELAY_DERIVED) are excluded. Results are ordered by the most recent result creation date (createdAtofIndividualResultorTeamResult) descending and, as a tiebreaker, byiddescending.This query selects only IDs to avoid the PostgreSQL restriction that ORDER BY expressions must appear in the SELECT list when DISTINCT is used, which occurs when fetching collections with JOIN FETCH. Use
findRacesByIdInWithCoursesAndEvent(Collection)to load the full entity graph for the returned IDs.- Parameters:
pageable- the pagination information; usePageRequest.of(0, limit)to retrieve the most recent N races- Returns:
- a
Pageof race IDs
-
findRacesByIdInWithCoursesAndEvent
@Query("SELECT DISTINCT r FROM Race r LEFT JOIN FETCH r.courses c LEFT JOIN FETCH c.event WHERE r.id IN :ids") List<Race> findRacesByIdInWithCoursesAndEvent(@Param("ids") Collection<Long> ids)
-