[英]one-to-many double mapping in hibernate
我想使用一對多關系來映射2個實體。
這兩個實體是airport和flight_schedule。
一個機場可以有許多航班時刻表。
但是,一個航班時刻表可以有2個機場。 出發機場和到達機場都是他們。
因此,我想使它們兩次一對多關聯。
我的意思是,Airport和flight_schedule具有兩次相同的關系。
希望您能了解這種情況。
所以,我所做的是:
機場.java
@Entity
@Table
public class Airport implements java.io.Serializable {
private Integer airportId;
private String name;
private String country;
private String city;
private Set<String> gateway = new HashSet<String>(0);
private Set<FlightSchedule> depFlightSchedule = new HashSet<FlightSchedule>(0);
private Set<FlightSchedule> arrFlightSchedule = new HashSet<FlightSchedule>(0);
public Airport() {
}
public Airport(String name, String country, String city) {
this.name = name;
this.country = country;
this.city = city;
}
public Airport(String name, String country, String city, Set<String> gateway, Set<FlightSchedule> depFlightSchedule, Set<FlightSchedule> arrFlightSchedule) {
this.name = name;
this.country = country;
this.city = city;
this.gateway = gateway;
this.depFlightSchedule = depFlightSchedule;
this.arrFlightSchedule = arrFlightSchedule;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "airport_id", unique = true, nullable = false)
public Integer getAirportId() {
return this.airportId;
}
.... getters and setters ....
@ElementCollection
@JoinTable(name = "gateway", joinColumns = @JoinColumn(name = "airport_id"))
@Column(name = "gateway_no")
public Set<String> getGateway() {
return this.gateway;
}
public void SetGateway(Set<String> gateway) {
this.gateway = gateway;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "depAirport")
public Set<FlightSchedule> getDepFlightSchedule() {
return this.depFlightSchedule;
}
public void setDepFlightSchedule(Set<FlightSchedule> depFlightSchedule) {
this.depFlightSchedule = depFlightSchedule;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "arrAirport")
public Set<FlightSchedule> getArrFlightSchedule() {
return this.arrFlightSchedule;
}
public void setArrFlightSchedule(Set<FlightSchedule> arrFlightSchedule) {
this.arrFlightSchedule = arrFlightSchedule;
}
}
最后兩個'@OneToMany'是airport和flight_schedule之間的關系。
另外,下面是FlightSchedule.java
@Entity
@Table
public class FlightSchedule implements java.io.Serializable {
private Integer flightscheduleId;
private Date depDay;
private Date depTime;
private Date arrDay;
private Date arrTime;
private Double flightTime;
private Set<BoardingPass> boardingPasses = new HashSet<BoardingPass>(0);
private Airplane airplane;
private Airport depAirport;
private Airport arrAirport;
public FlightSchedule() {
}
public FlightSchedule(Date depDay, Date depTime, Date arrDay, Date arrTime) {
this.depDay = depDay;
this.depTime = depTime;
this.arrDay = arrDay;
this.arrTime = arrTime;
}
public FlightSchedule(Date depDay, Date depTime, Date arrDay, Date arrTime, Double flightTime) {
this.depDay = depDay;
this.depTime = depTime;
this.arrDay = arrDay;
this.arrTime = arrTime;
this.flightTime = flightTime;
}
public FlightSchedule(Date depDay, Date depTime, Date arrDay, Date arrTime, Double flightTime,
Set<BoardingPass> boardingPasses, Airplane airplane, Airport depAirport, Airport arrAirport) {
this.depDay = depDay;
this.depTime = depTime;
this.arrDay = arrDay;
this.arrTime = arrTime;
this.flightTime = flightTime;
this.boardingPasses = boardingPasses;
this.airplane = airplane;
this.depAirport = depAirport;
this.arrAirport = arrAirport;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getFlightscheduleId() {
return this.flightscheduleId;
}
public void setFlightscheduleId(Integer flightscheduleId) {
this.flightscheduleId = flightscheduleId;
}
....getters and setters...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "airport_id", nullable = false)
public Airport getDepAirport() {
return this.depAirport;
}
public void setDepAirport (Airport depAirport) {
this.depAirport = depAirport;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "airport_id", nullable = false)
public Airport getArrAirport() {
return this.arrAirport;
}
public void setArrAirport (Airport arrAirport) {
this.arrAirport = arrAirport;
}
}
如上所述,最后兩個'@ManyToOne'是airport和flight_schedule之間的關系。
通過這段代碼,我得到了一個錯誤,即“ MappingException”。
它說RootClass(PersistentClass).checkColumnDuplication(Set,Iterator)。
我怎樣看待這種關系? 我該如何解決這個錯誤?
您說一個航班時刻表有兩個不同的機場:
但是,一個航班時刻表可以有2個機場。 出發機場和到達機場都是他們。
但是,您嘗試將depAirport
和arrAirport
都arrAirport
到同一列airport_id
。 無論表中的外鍵列如何,都應該對其進行更改,例如分別為@JoinColumn(name="dep_airport_id")
和@JoinColumn(name="arr_airport_id")
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.