0

I have main table merchants and second table terminals:

Merchant table:

@Entity
@Table
public class Merchants {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private int id;

    @Column
    private String name;

    @Column
    private String login;
}

Terminal table:

@Entity
@Table
public class Terminals {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private int id;

    @Column
    private int merchant_id;

    @Column
    private String mode;
}

I want to set merchant_id into table Terminals. I suppose that many to many will be proper relation. How I can create it?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • When you map an entity relationship in JPA, you do so via the *entities*. You do not map foreign key columns directly. You can find multiple examples [here on SO](https://stackoverflow.com/search?q=%5Bjava%5D+jpa+many+to+many+score%3A5+is%3Aquestion). – John Bollinger Jul 26 '18 at 20:46
  • @JohnBollinger can you paste official answer so I can up vote it? – Peter Penzov Jul 26 '18 at 20:48
  • What is there about the many many JPA documents on the internet that you cant understand? –  Jul 27 '18 at 06:10

1 Answers1

1

If you have a Join Table:

On Merchants class:

 @ManyToMany
 @JoinTable(name="MER_TER", joinColumns=@JoinColumn(name="MERCH_ID", referencedColumnName="id"),
  inverseJoinColumns=@JoinColumn(name="TERM_ID", referencedColumnName="id"))
  private List<Terminals> terminalsList;

On Terminals class:

@ManyToMany(mappedBy="terminalsList")
private List<Merchants> merchantsList;

Page of reference: link

If you don't have a Join Table, try to look here: https://stackoverflow.com/a/25018992

carmelolg
  • 493
  • 5
  • 17