- Fork this repo.
- Clone your fork to your local machine.
- Solve the challenges.
- Upon completion, add your solution to git.
- Then commit to git and push to your repo on GitHub.
- Make a pull request and paste the pull request link in the submission field in the Student Portal.
You’ve now learned how to create entity objects and use JPA repositories to query data. In this lab, you’ll apply those concepts by modeling an airline booking system.
By the end of this lab, you should be able to:
- Design JPA entities using enum values and basic attributes
- Set up and use
JpaRepositoryinterfaces - Create and use simple queries derived from method names
- Write clean, maintainable models using JPA best practices
We’re going to represent the booking system using data from the airline lab (Lab 3.01). Your mission is to design the classes and database structure for the following entities:
CustomerFlightFlightBooking
You'll also create an enum CustomerStatus.
You need to model and map the following:
Create an enum with these values:
GOLD, SILVER, NONEUse it inside your Customer entity. Don’t forget to annotate the field with @Enumerated(EnumType.STRING).
Attributes:
- A unique auto-generated ID
- A name (String)
- A status (enum
CustomerStatus) - Total miles flown (Integer)
Use proper annotations to map it as a table.
Attributes:
- Auto-generated ID
- Flight number (String)
- Aircraft (String)
- Total number of seats (Integer)
- Mileage (Integer)
Map it properly with JPA annotations.
Attributes:
- Auto-generated ID
- Customer ID (Integer)
- Flight ID (Integer)
For now, do not use
@ManyToOne. Just store the foreign keys as fields. We’ll introduce entity relationships in a future lesson.
- Define the four classes above with appropriate annotations.
- Create the corresponding
JpaRepositoryinterfaces for each entity. - Use CommandLineRunner to create at least 3 sample entries for each table. Example in the Main clas (can be also added in a @Configuration class):
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean CommandLineRunner run(CustomerRepository customerRepo, FlightRepository flightRepo, FlightBookingRepository bookingRepo) { return args -> { Customer alice = customerRepo.save(new Customer("Alice", CustomerStatus.GOLD, 120000)); Flight flight = flightRepo.save(new Flight("AB123", "Boeing 747", 300, 400)); bookingRepo.save(new FlightBooking(alice.getId(), flight.getId())); }; } }
- Add a query method in
CustomerRepositoryto find customers by status: - Add a query method in
FlightBookingRepositoryto find bookings by customer ID:
Try these extra tasks if you finish early:
- Add a method in
FlightBookingRepositoryto find bookings by flight ID - Add a method in
CustomerRepositoryto find customers with mileage over 100,000
Can I use CommandLineRunner to seed my data?
Yes! You can use @Bean CommandLineRunner to insert a few test entries when the application starts. Or use Postman to POST them via a REST controller.
Should I define relationships between entities?
Not yet! For now, just store the IDs (customerId, flightId) directly in FlightBooking. We’ll learn about @ManyToOne and other relationships in an upcoming lesson.
What if I get errors when saving data?
Make sure your entity classes have:
- An empty constructor
- Proper data types
@Entity,@Id, and@GeneratedValueannotations where needed
