I am trying to learn SpringBoot and Thymeleaf and I am getting a PropertyReferenceException. Most of the answers online have to do with naming errors, yet I have no naming errors; everything is in perfect camel case. Any ideas?
Relevant Code:
package com.example.demo.model;
import javax.persistence.*;
@Entity
public class Seat {
@Id
private String seatNum;
private String letter;
private String num;
@OneToOne
private Passenger passenger;
@ManyToOne
private Flight flight;
public Seat(String seatNum, String letter, String num) {
this.seatNum = seatNum;
this.letter = letter;
this.num = num;
}
public Seat() {
}
public Flight getFlight() {
return flight;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
public String getSeatNum() {
return seatNum;
}
public void setSeatNum(String seatNum) {
this.seatNum = seatNum;
}
public String getLetter() {
return letter;
}
public void setLetter(String letter) {
this.letter = letter;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Passenger getPassenger() {
return passenger;
}
public void setPassenger(Passenger passenger) {
this.passenger = passenger;
}
}
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List;
@Entity
public class Flight {
@Id
private String flightId;
private String source;
private String destination;
@OneToMany(mappedBy = "flight")
private List<Seat> seats;
public Flight() {
}
public Flight(String flightId, String source, String destination) {
this.flightId = flightId;
this.source = source;
this.destination = destination;
}
public List<Seat> getSeats() {
return seats;
}
public void setSeats(List<Seat> seats) {
this.seats = seats;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getFlightId() {
return flightId;
}
public void setFlightId(String flightId) {
this.flightId = flightId;
}
}
package com.example.demo.service;
import com.example.demo.model.Seat;
import com.example.demo.repository.SeatRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class SeatService {
@Autowired
private SeatRepository seatRepository;
public List<Seat> getAllSeats(String flightId){
List<Seat> seats = new ArrayList<>();
seatRepository.findByFlightId(flightId).forEach(seats::add);
return seats;
}
}
package com.example.demo.service;
import com.example.demo.model.Flight;
import com.example.demo.repository.FlightRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class FlightService {
@Autowired
private FlightRepository flightRepository;
public List<Flight> getAllFlights() {
List<Flight> flights = new ArrayList<>();
flightRepository.findAll().forEach(flights::add);
return flights;
}
public void addFlight(Flight flight){
flightRepository.save(flight);
}
}
package com.example.demo.controller;
import com.example.demo.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class SeatController {
@Autowired
private SeatService seatService;
@GetMapping("/{flightId}/seats")
public String getAllSeats(@PathVariable String flightId, Model model){
model.addAttribute("seats", seatService.getAllSeats(flightId));
return "seatsHTML";
}
}
package com.example.demo.controller;
import com.example.demo.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class FlightController {
@Autowired
private FlightService flightService;
@GetMapping("/flights")
public String getAllFlights(Model model){
model.addAttribute("flights", flightService.getAllFlights());
return "flights";
}
}
cpackage com.example.demo.repository;
import com.example.demo.model.Seat;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SeatRepository extends CrudRepository<Seat, String> {
public List<Seat> findByFlightId(String flightId);
}
package com.example.demo.repository;
import com.example.demo.model.Flight;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FlightRepository extends CrudRepository<Flight, String> {
}
**seatsHTML**
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Seats</title>
</head>
<body>
<table>
<tr th:each="seat: ${seats}">
<td th:text="${seat.getSeatNum()}"></td>
<td th:text="${seat.getFlight()}"></td>
<td th:text="${seat.getPassenger()}"></td>
</tr>
</table>
</body>
</html>
Error Stack Trace
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property id found for type Flight! Traversed path: Seat.flight.
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:447) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:471) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:366) ~[spring-data-commons-2.4.5.jar:2.4.5]
at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324) ~[na:1.8.0_261]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:348) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:249) ~[spring-data-commons-2.4.5.jar:2.4.5]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_261]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_261]
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_261]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_261]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_261]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_261]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_261]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_261]
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:250) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:383) ~[spring-data-commons-2.4.5.jar:2.4.5]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_261]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[na:1.8.0_261]
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_261]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_261]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_261]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_261]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_261]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_261]
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:384) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:95) ~[spring-data-commons-2.4.5.jar:2.4.5]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.4.5.jar:2.4.5]
... 71 common frames omitted
Process finished with exit code 1
What I have tried:
I have no idea what to try. I am a beginner to Spring Boot.