transiti0nary
transiti0nary

Reputation: 513

JPA Annotations - how to merge two fields into one column for a join table?

So I have two classes, a Route, and a BusStop. A Route 'Uses' two BusStop objects, one as a start, one as a destination. I need to have this relationship mapped via a join table, 'Uses'.

This join table doesn't need to distinguish if a BusStop is used for a start or destination. Basically I just need start and destination to become one column (with duplicates allowed) in this join table, so I can get a complete mapping of which routes use which stops.

Here is how I had it set up (assume getters and setters are there):

public class Route {
    @Id
    private String number;

    @Column(nullable = false)
    private int frequency;

    @ManyToOne
    @JoinTable(
        name = "Uses",
        joinColumns = @JoinColumn(name = "number"),
        inverseJoinColumns = @JoinColumn(name = "id")
    )
    private BusStop start;

    @ManyToOne
    @JoinTable(
        name = "Uses",
        joinColumns = @JoinColumn(name = "number"),
        inverseJoinColumns = @JoinColumn(name = "id")
    )
    private BusStop destination;
    ...
}

public class BusStop {
    @Id
    private int id;

    @Column(nullable = false)
    private String description;

    @OneToMany
    @JoinTable(
        name = "Uses",
        joinColumns = @JoinColumn(name = "id"),
        inverseJoinColumns = @JoinColumn(name = "number")
    )
    private Set<Route> routes;
    ...
}

Now the problem I'm having is that this results in a 'Uses' table that only ever has one of each Route id, though it successfully keeps duplicate stop ids. I'm assuming it has something to do with me trying to create two 'Uses' tables in the Route class?

So how exactly would be the correct way to go about this?

Upvotes: 0

Views: 2845

Answers (1)

ansh
ansh

Reputation: 696

I would go with the much cleaner way to do this.

Here are my entities.

package com.ansh.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Table(name = "routes")
@Getter @Setter
public class Route {

    @Id
    @Column(name = "number")
    private String number;

    @Column(name = "frequency", nullable = false)
    private int frequency;

    @ManyToOne
    @JoinColumn(name = "start_id")
    private BusStop start;

    @ManyToOne
    @JoinColumn(name = "destination_id")
    private BusStop destination;
}

AND

package com.ansh.entity;

import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "bus_stops")
@Getter @Setter
public class BusStop {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "description", nullable = false)
    private String description;

    @OneToMany(mappedBy = "start")
    private Set<Route> outgoingRoutes = Sets.newHashSet();

    @OneToMany(mappedBy = "destination")
    private Set<Route> incomingRoutes = Sets.newHashSet();

    public Set<Route> getRoutes() {
        return Sets.union(outgoingRoutes, incomingRoutes);
    }
}

Upvotes: 1

Related Questions