Issue
I've faced with an issue, I've declared the bidirectional @ManyToMany relation between my entities, but when I try to perform the select by repository, the @ManyToMany collection is always empty.
There is an example of my entities:
@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<>();
}
Also, I've enabled the SQL query logs, that hibernate generates, when I run it, the query works well. I've tried different guides and answers for the same issues, but I've no result with them.
What's the reason for this behavior? Maybe has somebody any ideas?
Solution
I've found the reason for this issue and the problem is in the primary keys of my tables, they aren't generated by sequence or some different way, as I understand Hibernate doesn't understand how to map this columns
Answered By - Eugene