Spring Data JPA / Hibernate ManyToMany 关系始终为空

问题描述

我遇到了一个问题,我已经声明了实体之间的双向 @ManyToMany 关系,但是当我尝试按存储库执行选择时,@ManyToMany 集合始终为空。

一个我的实体的例子:

@Data
@EqualsAndHashCode
@Entity
@Table(name = "provider",schema = "debt")
public class ProviderEntity {

    @Id
    @Column(name = "provider_id")
    private String providerId;
    @Column(name = "external_provider_id")
    private String externalProviderId;
    private String description;
    @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(
            name = "provider_supported_driver",schema = "debt",joinColumns = @JoinColumn(name = "provider_id",foreignKey = @ForeignKey(name = "FK_PSD_TO_P")),inverseJoinColumns = @JoinColumn(name = "driver_id",foreignKey = @ForeignKey(name = "FK_PSD_TO_D"))
    )
    private Set<DriverEntity> drivers = new HashSet<>();
}



@Data
@EqualsAndHashCode
@Entity
@Table(name = "driver",schema = "debt")
public class DriverEntity {

    @Id
    @Column(name = "driver_id")
    private String driverId;
    @Enumerated(EnumType.STRING)
    private DriverType type;
    private String description;
    @ManyToMany(mappedBy = "drivers")
    private Set<ProviderEntity> provider = new HashSet<>();
}

此外,我启用了休眠生成sql 查询日志,当我运行它时,查询运行良好。 对于相同的问题,我尝试了不同的指南和答案,但都没有结果。

这种行为的原因是什么?也许有人有任何想法?

解决方法

我已经找到了这个问题的原因,问题出在我的表的主键上,它们不是按顺序或以其他方式生成的,据我所知,Hibernate 不了解如何映射这些列