JPA多对多注解

多对多配置有两种形式,一种是单向配置,一种是双向配置。

例如:一个订单和多个商品的关联。


单向:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "xx_order_item_relate", joinColumns = {@JoinColumn(name = "oir_id")}, inverseJoinColumns = {@JoinColumn(name = "p_id")})

双向:

订单:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "xx_order_item_relate", joinColumns = {@JoinColumn(name = "oir_id")})

商品:

@OneToMany(mappedBy = "p_id", fetch = FetchType.LAZY)