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 Type
    Method
    Description
    Finds a Race by 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 a Page of IDs of non-derived races that have at least one IndividualResult or TeamResult recorded in the database.
    Returns a List of races matching the given IDs, with their courses and course events eagerly fetched to avoid N+1 queries when mapping to DTOs.

    Methods inherited from interface org.springframework.data.repository.CrudRepository

    count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, save

    Methods inherited from interface org.springframework.data.jpa.repository.JpaRepository

    deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlush

    Methods inherited from interface org.springframework.data.repository.ListCrudRepository

    findAll, findAllById, saveAll

    Methods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository

    findAll

    Methods inherited from interface org.springframework.data.repository.PagingAndSortingRepository

    findAll

    Methods 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 a Race by 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 Race with 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 a Page of IDs of non-derived races that have at least one IndividualResult or TeamResult recorded 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 (createdAt of IndividualResult or TeamResult) descending and, as a tiebreaker, by id descending.

      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; use PageRequest.of(0, limit) to retrieve the most recent N races
      Returns:
      a Page of 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)
      Returns a List of races matching the given IDs, with their courses and course events eagerly fetched to avoid N+1 queries when mapping to DTOs.

      The returned list may not preserve the order of the input IDs. The caller is responsible for restoring the desired order after fetching.

      Parameters:
      ids - the IDs of the races to fetch
      Returns:
      a List of races with courses and events loaded