我如何在Spring Boot / Hibernate中表示“多对多”关系?

时间:2019-05-21 00:07:39

标签: hibernate spring-boot orm many-to-many

考虑以下3个主表:

  1. 投标-主键“ id”和一些其他列
  2. 商品-主键“ id”和一些其他列
  3. 保险-主键“ id”和一些其他列

现在考虑以下联接表,其列名称引用相应的主表/联接表:

  1. Tender_Commodity-主键“ id”和外键“ Tender”和“ Commodity”
  2. Tender_Commodity_Insurance-主键“ id”和外键“ Insurance”和“ Tender_Commodity”

我只是不知道如何用Hibernate在Spring Boot(Java)中表示这一点。

  1. 尝试将组合键与@Embeddable一起使用
  2. 尝试了ManyToMany分别提供@JoinTable和@JoinColumns
  3. 也尝试了其他一些选择,但是以上两个是我认为可以使用的。.

为简单起见,我在上面粘贴了选项2的代码

df.query("value1 != value2").dropna()

Out[3035]:
     col1    col2  value1   value2
2  type_5  type_6  apples  oranges
public class Tender {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    //Omitted other attributes for brevity

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
    @JoinTable(name = "tender_commodity", joinColumns = @JoinColumn(name = "tender", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "commodity", referencedColumnName = "id"))
    private Set<Commodity> commodity;
}
public class Commodity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    //Omitted other attributes for brevity
}
public class TenderCommodity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "commodity")
    private Commodity commodity;

    @ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "tender")
    private Tender tender;
}

我在上面的实现中遇到了各种各样的错误消息,范围从:

  1. java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:二进制逻辑运算符的左右两侧不兼容
  2. 对于多对多映射,外键必须具有与引用的主键相同的列数

0 个答案:

没有答案