Interface EventRepository

All Superinterfaces:
org.springframework.data.repository.CrudRepository<Event,Long>, org.springframework.data.jpa.repository.JpaRepository<@NonNull Event,@NonNull Long>, org.springframework.data.repository.ListCrudRepository<Event,Long>, org.springframework.data.repository.ListPagingAndSortingRepository<Event,Long>, org.springframework.data.repository.PagingAndSortingRepository<Event,Long>, org.springframework.data.repository.query.QueryByExampleExecutor<Event>, org.springframework.data.repository.Repository<Event,Long>

@Repository public interface EventRepository extends org.springframework.data.jpa.repository.JpaRepository<@NonNull Event,@NonNull Long>
The EventRepository provides database access for Event entities.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a List of all events ordered by their startDate descending.
    org.springframework.data.domain.Page<Event>
    findByEventYear(int year, org.springframework.data.domain.Pageable pageable)
    Returns a Page of events whose startDate falls within the given year, ordered by their start date descending.
    Finds an Event by its ID and year with the full object graph eagerly fetched (courses, distances, and races).
    Returns all distinct years extracted from the startDate of existing events, ordered descending.
    Returns a List of events matching the given IDs, with their courses eagerly fetched to avoid N+1 queries when mapping to DTOs that require sport codes.
    org.springframework.data.domain.Page<Long>
    findMostRecentAddedEventIds(org.springframework.data.domain.Pageable pageable)
    Returns a Page of IDs of the most recently added events, ordered by their createdAt descending and, as a tiebreaker, by id descending.

    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

    • findByEventYear

      @Query(value="SELECT DISTINCT e FROM Event e LEFT JOIN FETCH e.courses WHERE YEAR(e.startDate) = :year ORDER BY e.startDate DESC", countQuery="SELECT COUNT(DISTINCT e) FROM Event e WHERE YEAR(e.startDate) = :year") org.springframework.data.domain.Page<Event> findByEventYear(@Param("year") int year, org.springframework.data.domain.Pageable pageable)
      Returns a Page of events whose startDate falls within the given year, ordered by their start date descending.
      Parameters:
      year - the year to filter events by
      pageable - the pagination information
      Returns:
      a Page of events for the given year, ordered by start date descending
    • findByIdAndYearWithGraph

      @Query("SELECT DISTINCT e FROM Event e LEFT JOIN FETCH e.courses c LEFT JOIN FETCH c.distance LEFT JOIN FETCH c.races LEFT JOIN FETCH e.eventFiles WHERE e.id = :id AND YEAR(e.startDate) = :year") Optional<Event> findByIdAndYearWithGraph(@Param("id") Long id, @Param("year") int year)
      Finds an Event by its ID and year with the full object graph eagerly fetched (courses, distances, and races). This avoids N+1 queries and ensures Hibernate constructs the correct concrete subclass instances for the joined-table inheritance hierarchy of Distance.
      Parameters:
      id - the event ID
      year - the year to filter events by
      Returns:
      the event with its associated courses, distances, and races, or empty if not found
    • findDistinctYears

      @Query("SELECT DISTINCT YEAR(e.startDate) FROM Event e ORDER BY YEAR(e.startDate) DESC") List<Integer> findDistinctYears()
      Returns all distinct years extracted from the startDate of existing events, ordered descending.
      Returns:
      a list of distinct years in descending order
    • findEventsByIdInWithCourses

      @Query("SELECT DISTINCT e FROM Event e LEFT JOIN FETCH e.courses WHERE e.id IN :ids") List<Event> findEventsByIdInWithCourses(@Param("ids") Collection<Long> ids)
      Returns a List of events matching the given IDs, with their courses eagerly fetched to avoid N+1 queries when mapping to DTOs that require sport codes.

      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 events to fetch
      Returns:
      a List of events with courses loaded
    • findAllByOrderByStartDateDesc

      List<Event> findAllByOrderByStartDateDesc()
      Returns a List of all events ordered by their startDate descending.
      Returns:
      a List of events ordered by start date descending
    • findMostRecentAddedEventIds

      @Query(value="SELECT e.id FROM Event e ORDER BY e.createdAt DESC, e.id DESC", countQuery="SELECT COUNT(e.id) FROM Event e") org.springframework.data.domain.Page<Long> findMostRecentAddedEventIds(org.springframework.data.domain.Pageable pageable)
      Returns a Page of IDs of the most recently added events, ordered by their createdAt 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 findEventsByIdInWithCourses(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 events
      Returns:
      a Page of event IDs