[英]Spring jpa hibernate how to map a relationship to multiple objects of the same entity
我正在使用 Spring Boot 創建一個航空公司應用程序,並且有一個名為 Flight 的實體,它具有同一實體的兩個屬性:
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
}
機場具有航班列表的一對多關系。
如何正確映射這種關系? Spring現在給了我錯誤:
映射通過引用未知的目標實體屬性(機場航班)
提前致謝。
在機場實體內,mappedBy 引用應如下所示...
@OneToMany(mappedBy = "startLocation")
@OneToMany(mappedBy = "目的地")
在Airport Entity 中定義一個關系,並指定屬性mappedBy=""。 MappedBy 基本上告訴休眠不要創建另一個連接表,因為該關系已經被該關系的相反實體映射。
這基本上是指 Flight Entity 中使用的變量,如 startLocation 和目的地。 它應該是這樣的:-
public class Airport {
...
@OneToMany(mappedBy = "startLocation")
private Flight flight_departure_location;
@OneToMany(mappedBy = "destination")
private Flight flight_destination;
}
它應該是這樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.