[英]one-to-many double mapping in hibernate
I want to map 2 entities using one-to-many relationship. 我想使用一对多关系来映射2个实体。
The two entities are airport and flight_schedule. 这两个实体是airport和flight_schedule。
one airport can have many flight schedules. 一个机场可以有许多航班时刻表。
But, one flight schedule can have 2 airports. 但是,一个航班时刻表可以有2个机场。 departure airport, and arrived airport are them.
出发机场和到达机场都是他们。
So, I want to make them one-to-many association twice. 因此,我想使它们两次一对多关联。
I mean, airport and flight_schedule have the twice same relationship. 我的意思是,Airport和flight_schedule具有两次相同的关系。
I hope you can understand this situation. 希望您能了解这种情况。
So, what I did is : 所以,我所做的是:
Airport.java 机场.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;
}
}
The last two '@OneToMany' is the very relationship between airport and flight_schedule. 最后两个'@OneToMany'是airport和flight_schedule之间的关系。
Also, below is FlightSchedule.java 另外,下面是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;
}
}
As above, the last two '@ManyToOne' is the very relationship between airport and flight_schedule. 如上所述,最后两个'@ManyToOne'是airport和flight_schedule之间的关系。
Through this code, I got an error which is 'MappingException'. 通过这段代码,我得到了一个错误,即“ MappingException”。
It says RootClass(PersistentClass).checkColumnDuplication(Set, Iterator). 它说RootClass(PersistentClass).checkColumnDuplication(Set,Iterator)。
How can I figure that relation? 我怎样看待这种关系? How can I solve this error?
我该如何解决这个错误?
You say that a flight schedule has two different airports: 您说一个航班时刻表有两个不同的机场:
But, one flight schedule can have 2 airports.
但是,一个航班时刻表可以有2个机场。 departure airport, and arrived airport are them.
出发机场和到达机场都是他们。
However, you are trying to map both depAirport
and arrAirport
to the same column airport_id
. 但是,您尝试将
depAirport
和arrAirport
都arrAirport
到同一列airport_id
。 You should change it for whatever are the foreign key columns in the table, like @JoinColumn(name="dep_airport_id")
and @JoinColumn(name="arr_airport_id")
, respectively. 无论表中的外键列如何,都应该对其进行更改,例如分别为
@JoinColumn(name="dep_airport_id")
和@JoinColumn(name="arr_airport_id")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.