I am creating an airline application using Spring boot, and there is an entity called Flight that has two attributes of the same entity:
public class Flight {
// more code here
@ManyToOne
@JoinColumn(name = "airport_id", referencedColumnName = "id")
private Airport startLocation;
@ManyToOne
@JoinColumn(name = "airport_id", referencedColumnName = "id")
private Airport destination;
// more code here
}
An airport has a OneToMany relationship of a List of flights.
How can I correctly map this relationship? Spring now gives me the error:
mappedBy reference an unknown target entity property (airport-flights)
Thanks in advance.
CodePudding user response:
Inside the airport entity, the mappedBy reference should be like the following...
@OneToMany(mappedBy = "startLocation")
@OneToMany(mappedBy = "destination")
CodePudding user response:
Define a relationship in Airport Entity, and specify the property mappedBy="". MappedBy basically tells the hibernate not to create another join table as the relationship is already being mapped by the opposite entity of this relationship.
That basically refers to the variables used in Flight Entity like startLocation and destination. It should look like this:-
public class Airport {
...
@OneToMany(mappedBy = "startLocation")
private Flight flight_departure_location;
@OneToMany(mappedBy = "destination")
private Flight flight_destination;
}
It should be something like this.