休眠中的一对多双映射
one-to-many double mapping in hibernate
我想使用一对多关系映射 2 个实体。
这两个实体是机场和flight_schedule。
一个机场可以有多个航班时刻表。
但是,一个航班时刻表可以有 2 个机场。出发机场和到达机场都是他们。
所以,我想使它们成为一对多关联两次。
我的意思是,机场和 flight_schedule 有两次相同的关系。
希望您能理解这种情况。
所以,我所做的是:
Airport.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'是机场和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)。
我如何确定这种关系?我该如何解决这个错误?
你说一个航班时刻表有两个不同的机场:
But, one flight schedule can have 2 airports. departure airport, and arrived airport are them.
但是,您试图将 depAirport
和 arrAirport
映射到同一列 airport_id
。您应该针对 table 中的外键列分别更改它,例如 @JoinColumn(name="dep_airport_id")
和 @JoinColumn(name="arr_airport_id")
。
我想使用一对多关系映射 2 个实体。
这两个实体是机场和flight_schedule。
一个机场可以有多个航班时刻表。
但是,一个航班时刻表可以有 2 个机场。出发机场和到达机场都是他们。
所以,我想使它们成为一对多关联两次。
我的意思是,机场和 flight_schedule 有两次相同的关系。
希望您能理解这种情况。
所以,我所做的是:
Airport.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'是机场和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)。
我如何确定这种关系?我该如何解决这个错误?
你说一个航班时刻表有两个不同的机场:
But, one flight schedule can have 2 airports. departure airport, and arrived airport are them.
但是,您试图将 depAirport
和 arrAirport
映射到同一列 airport_id
。您应该针对 table 中的外键列分别更改它,例如 @JoinColumn(name="dep_airport_id")
和 @JoinColumn(name="arr_airport_id")
。