I am trying to build a REST API for an airport app. The issue is when I try to update my Airplane
which has a many-to-one relationship with Airline
, I try to check if the id of the airline which the airplane has a relationship with is equal to id provided during the request
This is the updateAirplane
method:
@Override
public AirplaneDto updateAirplane(long airlineId, long airplaneId, AirplaneDto airplaneDto) {
Airline airline = airlineRepository.findById(airlineId).orElseThrow(
() -> new ResourceNotFoundException("Airline", "id", airlineId));
Airplane airplane = airplaneRepository.findById(airplaneId).orElseThrow(
() -> new ResourceNotFoundException("Airplane", "id", id));
if(!airplane.getAirline().getId().equals(airline.getId())) {
throw new AirportAPIException(HttpStatus.BAD_REQUEST, "Airplane does not belong to the Airline");
}
return null
}
The equals()
above gives me "Cannot resolve method 'equals(long)'" error and further adds that "No candidates found for method call airplane.getAirline().getId().equals(airline.getId())."
This are the entities for airplane and airline respectively
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "airplane")
public class Airplane {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String aircraftRegistration;
private String model;
private long capacity;
private String code;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "airline_id", nullable = false)
private Airline airline;
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "Airline", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"})})
public class Airline {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String headquarters;
@OneToMany(mappedBy = "airline", cascade = CascadeType.ALL, orphanRemoval = true )
private Set<Airplane> airplanes = new HashSet<>();
}
compare aireline and airplane Ids directly, with airplane.getAirline().getId !=airline.getId()