title: Multiple foreign keys in one mapping class Hibernate+JPA+Spring
body: <p>I have 2 classes called Client and Movie. I want to have a class RentedMovie that would hold foreign key of a Client and the foreign key of a Book and it would also have its own id. So far my classes look the same:</p>
  
  <p>Client:</p>
  
  <pre><code>@Entity
  @Table(name="Client")
  public class Client implements Serializable {
  
      @Id
      @Column(name="id", nullable= false)
      private Integer id;
  
      @Column(name="age", nullable = false)
      private Integer age;
  
      @Column(name="numberofrentals", nullable = false)
      private Integer numberofrentals;
  
      @Column(name="name", nullable = false)
      private String name;
  
      @Column(name="address", nullable = false)
      private String address;
  
      public Client() {
      }
  
      public Client(Integer id, Integer age, Integer numberofrentals, String name, String address) {
  
          this.id = id;
          this.age = age;
          this.numberofrentals = numberofrentals;
          this.name = name;
          this.address = address;
      }
  
      public Integer getId() {
  
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public Integer getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
      public Integer getnumberofrentals() {
          return numberofrentals;
      }
  
      public void setnumberofrentals(int numberofrentals) {
          this.numberofrentals = numberofrentals;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getAddress() {
          return address;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          Client client = (Client) o;
  
          if (age != client.age) return false;
          if (numberofrentals != client.numberofrentals) return false;
          if (!id.equals(client.id)) return false;
          if (!name.equals(client.name)) return false;
          return address.equals(client.address);
  
      }
  
      @Override
      public int hashCode() {
          int result = id.hashCode();
          result = 31 * result + age;
          result = 31 * result + numberofrentals;
          result = 31 * result + name.hashCode();
          result = 31 * result + address.hashCode();
          return result;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      @Override
      public String toString() {
          return "Client{" +
                  "id=" + id +
                  ", age=" + age +
                  ", numberofrentals=" + numberofrentals +
                  ", name='" + name + '\'' +
                  ", address='" + address + '\'' +
                  '}';
      }
  }
  </code></pre>
  
  <p>Movie:</p>
  
  <pre><code>@Entity
  @Table(name="movie")
  public class Movie  implements Serializable {
  
      @Id
      @Column(name="id", nullable=false)
      private Integer id;
  
      @Column(name="name", nullable = false)
      private String name;
  
      @Column(name="numberofrentals", nullable=false)
      private Integer numberofrentals;
  
      @Column(name="director", nullable = false)
      private String director;
  
      @Column(name="year", nullable = false)
      private Integer year;
  
      public Movie() {
      }
  
      public Movie(Integer id, String name, Integer numberofrentals, String director, Integer year) {
          this.id = id;
          this.name = name;
          this.numberofrentals = numberofrentals;
          this.director = director;
          this.year = year;
      }
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getDirector() {
          return director;
      }
  
      public void setDirector(String director) {
          this.director = director;
      }
  
      public Integer getYear() {
          return year;
      }
  
      public void setYear(Integer year) {
          this.year = year;
      }
  
      public Integer getNumberofrentals() {
          return numberofrentals;
      }
  
      public void setNumberofrentals(Integer numberofrentals) {
          this.numberofrentals = numberofrentals;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          Movie movie = (Movie) o;
  
          if (!id.equals(movie.id)) return false;
          if (!name.equals(movie.name)) return false;
          if (!numberofrentals.equals(movie.numberofrentals)) return false;
          if (!director.equals(movie.director)) return false;
          return year.equals(movie.year);
  
      }
  
      @Override
      public int hashCode() {
          int result = id.hashCode();
          result = 31 * result + name.hashCode();
          result = 31 * result + numberofrentals.hashCode();
          result = 31 * result + director.hashCode();
          result = 31 * result + year.hashCode();
          return result;
      }
  
      @Override
      public String toString() {
          return "Movie{" +
                  "id=" + id +
                  ", name='" + name + '\'' +
                  ", numberofrentals=" + numberofrentals +
                  ", director='" + director + '\'' +
                  ", year=" + year +
                  '}';
      }
  }
  </code></pre>
  
  <p>And the rented class which is jsut normal right now:</p>
  
  <pre><code>@Entity
  @Table(name="rented")
  public class Rented implements Serializable {
      @Id
      @Column(name="id", nullable= false)
      private Integer id;
  
      @Column(name="movieID", nullable = false)
      private Integer movieID;
  
      @Column(name="clientID", nullable = false)
      private Integer clientID;
  
      public Rented(Integer id, Integer movieID, Integer clientID) {
          this.id = id;
          this.movieID = movieID;
          this.clientID = clientID;
      }
  
      public Rented() {
      }
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public Integer getMovieID() {
          return movieID;
      }
  
      public void setMovieID(Integer movieID) {
          this.movieID = movieID;
      }
  
      public Integer getClientID() {
          return clientID;
      }
  
      public void setClientID(Integer clientID) {
          this.clientID = clientID;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          Rented rented = (Rented) o;
  
          if (!id.equals(rented.id)) return false;
          if (!movieID.equals(rented.movieID)) return false;
          return clientID.equals(rented.clientID);
  
      }
  
      @Override
      public int hashCode() {
          int result = id.hashCode();
          result = 31 * result + movieID.hashCode();
          result = 31 * result + clientID.hashCode();
          return result;
      }
  
      @Override
      public String toString() {
          return "Rented{" +
                  "id=" + id +
                  ", movieID=" + movieID +
                  ", clientID=" + clientID +
                  '}';
      }
  }
  </code></pre>
  
  <p>What do I really need to change? I need to give the Rented class a movie and a client? Between client and book it's a one to many relationship. A book can be rented only once, a client can rent multiple books. Do I have to keep lists somewhere or how would the rented class look like and what else do I have to change in the other 2?</p>