在JPA中创建表之间的关系

时间:2018-07-26 20:33:28

标签: java jpa jpa-2.0

我有主要的桌子商人和第二个桌子终端:

商人桌:

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

端子台:

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

我要在表格Terminals中设置merchant_id。我想多对多是正确的关系。如何创建它?

1 个答案:

答案 0 :(得分:1)

如果您有一个联接表:

关于商人类:

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

在终端类上:

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

参考页:link

如果没有联接表,请尝试在此处查看:https://stackoverflow.com/a/25018992