This project is a RESTful Flight Booking system built with Spring Boot, Spring Data JPA, and MySQL. It offers endpoints for managing and querying flight data with various filters (like source, destination, class, price, duration, etc.).
entity/FlightData.javaโ JPA Entity for flight data.repository/FlightDataRepository.javaโ Spring Data JPA repository interface.service/FlightDataService.javaโ Business logic layer.controller/FlightDataController.javaโ REST API endpoints.
List of all the topics and subtopics (features/concepts) covered in this project:
-
JPA Core
@Entity,@Id,@ColumnusageJpaRepositoryfor basic CRUD operations- Auto table generation and object-relational mapping
-
Spring Data JPA Queries
- Derived Query Methods
findBySourceCityAndDestinationCityfindByPriceLessThan,findByPriceBetweenfindTop5ByOrderByPriceAsc, etc.
- Custom JPQL Queries
@Querywith JPQL (e.g.,findFlightsByRoute)- Pattern matching (
LIKE, time range filters)
- Native SQL Queries
@Query(..., nativeQuery = true)for performance or complex joins- Queries with
LIMIT,ORDER BY, etc.
- Derived Query Methods
-
Spring Boot Service Layer
- Business logic methods for filtering and updating
@Transactionalmethods for batch updates/deletes
-
Controller Layer
- REST APIs using
@RestControllerand@GetMapping,@PostMapping, etc. - Handling of
Optional<FlightData> - Use of
ResponseEntity.ok(),notFound(),noContent().build()
- REST APIs using
Used for flexible response handling in controllers:
@GetMapping("/id/{id}")
public ResponseEntity<FlightData> getFlightById(@PathVariable Long id) {
Optional<FlightData> flight = flightDataService.getFlightById(id);
return flight.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}๐น Explanation:
Optional<FlightData>: Represents the presence or absence of a flight.ResponseEntity.ok(flight)= returns HTTP200 OKwith the flight data.ResponseEntity.notFound().build()= returns HTTP404 Not Foundif no flight exists.ResponseEntity.noContent().build()= returns HTTP204 No Content(used when operation succeeds but no data is returned).
You can write methods based on Spring naming conventions and JPA will auto-generate the SQL:
List<FlightData> findBySourceCityAndDestinationCity(String source, String destination);
List<FlightData> findByPriceLessThan(Double maxPrice);
List<FlightData> findTop5ByOrderByPriceAsc();โก๏ธ No need for @Query. Spring understands the method name and builds the query.
Use JPQL when:
- You need custom filtering.
- You want to select specific fields.
- You need joins, aggregates, or more advanced logic.
@Query("SELECT f FROM FlightData f WHERE f.sourceCity = :source AND f.destinationCity = :destination AND f.flightClass = :class")
List<FlightData> findFlightsByRoute(@Param("source") String source,
@Param("destination") String destination,
@Param("class") String flightClass);Another example โ using pattern matching:
@Query("SELECT f FROM FlightData f WHERE f.departureTime LIKE :timePattern")
List<FlightData> findFlightsByDepartureTimePattern(@Param("timePattern") String timePattern);For performance or DB-specific queries:
@Query(value = "SELECT * FROM flight_data WHERE price < :maxPrice AND stops <= :maxStops ORDER BY price ASC LIMIT :limit", nativeQuery = true)
List<FlightData> findBestDeals(@Param("maxPrice") Double maxPrice,
@Param("maxStops") Integer maxStops,
@Param("limit") Integer limit);Spring Data provides automatic count and exists queries:
Long countByAirline(String airline);
Boolean existsByFlight(String flightNumber);Bulk deletion or updates are handled with @Transactional:
@Transactional
void deleteByPriceGreaterThan(Double price);@Transactional
public void updateFlightPrices(String airline, Double discountPercentage) {
List<FlightData> flights = flightDataRepository.findByAirline(airline);
for (FlightData flight : flights) {
flight.setPrice(flight.getPrice() * (1 - discountPercentage / 100));
}
flightDataRepository.saveAll(flights);
}GET /flights/id/101Returns:
{
"id": 101,
"flight": "AI-101",
"sourceCity": "Delhi",
"destinationCity": "Mumbai",
"price": 5200.0
}GET /flights/search?source=Delhi&destination=Mumbai&class=EconomyUses this JPQL method:
@Query("SELECT f FROM FlightData f WHERE f.sourceCity = :source AND f.destinationCity = :destination AND f.flightClass = :class")
List<FlightData> findFlightsByRoute(@Param("source") String source,
@Param("destination") String destination,
@Param("class") String flightClass);| Use Case | JPQL (@Query) |
Derived Method |
|---|---|---|
| Complex filters with multiple fields | โ | โ |
| LIKE, BETWEEN, subqueries | โ | โ |
| Simple field = value lookups | โ | โ |
| Top N sorting | โ | โ
(e.g. findTop5ByOrderByPriceAsc) |
- Spring Boot Starter Web
- Spring Boot Starter Data JPA
- MySQL Driver
- Lombok (optional)
Dataset used : https://www.kaggle.com/datasets/rohitgrewal/airlines-flights-data/