Hummm,
I need to store the order number of each station in the route
Why do you do not use a List instead. You just have to create a linked table
@Entity
public class Route {
private Integer id;
private List<RouteAndStation> stationList = new ArrayList<RouteAndStation>();
@Id
public Integer getId() {
return this.id;
}
@OneToMany(mappedBy="route")
@Cascade(SAVE_UPDATE)
public List<RouteAndStation> getStationList() {
return this.stationList;
}
@Transient
public List<Station> getStationByOrderNumber() {
Collections.sort(this.stationList, new Comparator<RouteAndStation>() {
public int compare(RouteAndStation o1, RouteAndStation o2) {
return o1.getOrderNumber().compareTo(o2.getOrderNumber());
}
});
List<Station> sList = new ArrayList();
for(RouteAndStation routeAndStation: this.stationList)
sList.add(routeAndStation.getStation());
return sList;
}
public void addStation(Station station) {
RouteAndStation routeAndStation = new RouteAndStation();
routeAndStation.addStation(this, station, getStationList().size());
getStationList().add(routeAndStation);
}
}
And Station
@Entity
public class Station {
private Integer id;
private List<RouteAndStation> routeList = new ArrayList<RouteAndStation>();
@Id
public Integer getId() {
return this.id;
}
@OneToMany(mappedBy="station")
// DO YOU NEDD CASCADE ???
@Cascade(SAVE_UPDATE)
public List<RouteAndStation> getRouteList() {
return this.routeList;
}
}
RouteAndStation is implemented as follows
@Entity
public class RouteAndStation {
private Route route;
private Station station;
private Integer stationNumber;
private RouteAndStationId routeAndStationId;
@EmbeddedId
public RouteAndStationId getRouteAndStationId() {
return this.routeAndStationId;
}
public void addStation(Route route, Station station, Integer stationNumber) {
this.route = route;
this.station = station;
this.stationNumber = stationNumber;
setRouteAndStationId(new RouteAndStationId(route.getId(), station.getId));
}
@ManyToOne(fetch=LAZY)
@JoinColumn(name="ROUTE_ID", insertable=false, updateable=false)
public Route getRoute() {
return this.route;
}
@ManyToOne(fetch=LAZY)
@JoinColumn(name="STATION_ID", insertable=false, updateable=false)
public Station getStation() {
return this.station;
}
@Embeddable
public static class RouteAndStationId implements Serializable {
private Integer routeId;
private Integer stationId;
// required no-arg constructor
public RouteAndStationId() {}
public RouteAndStationId(Integer routeId, Integer stationId) {
this.routeId = routeId;
this.stationId = stationId;
}
@Column(name="ROUTE_ID")
public Integer getRouteId {
return this.routeId;
}
@Column(name="STATION_ID")
public Integer getStationId {
return this.stationId;
}
public boolean equals(Object o) {
if(o == null)
return false;
if(!(o instanceof RouteAndStationId))
return false;
RouteAndStationId other = (RouteAndStationId) o;
if(!(getRouteId().equals(other.getRouteId())))
return false;
if(!(getStationId().equals(other.getStationId())))
return false;
return true;
}
}
}
I hope it can be useful to you
regards,