我想使用JPA anotations创建表(RoutePlace)与其自身的多对多关系。我想加入的表的奇点是它有一个复合键。
示例:https://h4losw2.files.wordpress.com/2008/10/tables.png
有什么建议吗?
提前致谢
答案 0 :(得分:2)
我不确定我的问题是否正确,但你的意思是这样的:
@IdClass(RoutePlacePK.class)
@Entity
public class RoutePlace {
private Collection<RoutePlace> route;
@ManyToMany(mappedBy = "place")
public Collection<RoutePlace> getRoute() {
return route;
}
public void setRoute(Collection<RoutePlace> route) {
this.route = route;
}
private Collection<RoutePlace> place;
@ManyToMany
public Collection<RoutePlace> getPlace() {
return place;
}
public void setPlace(Collection<RoutePlace> place) {
this.place = place;
}
}
复合主键包装类:
public class RoutePlacePK {
private int routeID;
private int placeID;
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RoutePlacePK that = (RoutePlacePK) o;
if (placeID != that.placeID) return false;
if (routeID != that.routeID) return false;
return true;
}
public int hashCode() {
int result;
result = routeID;
result = 31 * result + placeID;
return result;
}
}