views:

3541

answers:

1

I would like to create a many to many relationship of a table (RoutePlace) with itself using JPA anotations. The singularity of the table I want to join with itself is that it has a compound-key.

example: http://h4losw2.files.wordpress.com/2008/10/tables.png

Any suggestions?

Thanks in advance

A: 

I'm not sure if I get your question right, but do you mean something like this:

@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;
    }
}

The compound primary key wrapper class:

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;
    }
}
homaxto