1

我想使用 JPA 注释创建一个表(RoutePlace)与其自身的多对多关系。我想加入的表的独特之处在于它有一个复合键。

示例:https ://h4losw2.files.wordpress.com/2008/10/tables.png

替代文字

有什么建议么?

提前致谢

4

1 回答 1

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;
    }
}
于 2008-10-20T13:39:00.900 回答